Skip to content

Commit 14d7063

Browse files
committed
Test with a custom column name, plus README
1 parent 848e31f commit 14d7063

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

Diff for: Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ nominatim: tests/nominatim.db
1313
geocode-sqlite nominatim $^ innout_test \
1414
--location "{full}, {city}, {state} {postcode}" \
1515
--delay 1 \
16+
--raw \
1617
--user-agent "geocode-sqlite"
1718

1819
.PHONY: mapquest
1920
mapquest: tests/mapquest.db
2021
geocode-sqlite open-mapquest $^ innout_test \
2122
--location "{full}, {city}, {state} {postcode}" \
23+
--raw \
2224
--api-key "$(MAPQUEST_API_KEY)"
2325

2426
.PHONY: google
2527
google: tests/google.db
2628
geocode-sqlite googlev3 $^ innout_test \
2729
--location "{full}, {city}, {state} {postcode}" \
30+
--raw \
2831
--api-key "$(GOOGLE_API_KEY)" \
2932
--bbox 33.030551 -119.787326 34.695341 -115.832248
3033

@@ -33,19 +36,23 @@ bing: tests/bing.db
3336
geocode-sqlite bing $^ innout_test \
3437
--location "{full}, {city}, {state} {postcode}" \
3538
--delay 1 \
39+
--raw \
3640
--api-key "$(BING_API_KEY)"
3741

3842
.PHONY: mapbox
3943
mapbox: tests/mapbox.db
4044
geocode-sqlite mapbox $^ innout_test \
4145
--location "{full}, {city}, {state} {postcode}" \
4246
--delay 1 \
47+
--raw \
4348
--api-key "$(MAPBOX_API_KEY)"
4449

4550
.PHONY: opencage
4651
opencage: tests/opencage.db
4752
geocode-sqlite opencage $^ innout_test \
4853
--location "{full}, {city}, {state} {postcode}" \
54+
--delay '0.1' \
55+
--raw \
4956
--api-key "$(OPENCAGE_API_KEY)"
5057

5158
.PHONY: run

Diff for: README.md

+21
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,30 @@ From there, we have a set of options passed to every geocoder:
8484
- `longitude`: longitude column name
8585
- `geojson`: store results as GeoJSON, instead of in latitude and longitude columns
8686
- `spatialite`: store results in a SpatiaLite geometry column, instead of in latitude and longitude columns
87+
- `raw`: store raw geocoding results in a JSON column
8788

8889
Each geocoder takes additional, specific arguments beyond these, such as API keys. Again, [geopy's documentation](https://geopy.readthedocs.io/en/latest/#module-geopy.geocoders) is an excellent resource.
8990

91+
## Using SpatiaLite
92+
93+
The `--spatialite` flag will store results in a [geometry column](https://www.gaia-gis.it/gaia-sins/spatialite-cookbook-5/cookbook_topics.adminstration.html#topic_TABLE_to_SpatialTable), instead of `latitude` and `longitude` columns. This is useful if you're doing other GIS operations, such as using a [spatial index](https://www.gaia-gis.it/fossil/libspatialite/wiki?name=SpatialIndex). See the [SpatiaLite cookbook](https://www.gaia-gis.it/gaia-sins/spatialite-cookbook-5/index.html) and [functions list](https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html) for more of what's possible.
94+
95+
## Capturing additional geocoding data
96+
97+
Geocoding services typically return more data than just coordinates. This might include accuracy, normalized addresses or other context. This can be captured using the `--raw` flag. By default, this will add a `raw` column and store the full geocoding response as JSON. If you want to rename that column, pass a value, like `--raw custom_raw`.
98+
99+
The shape of this response object will vary between services. You can query specific values using [SQLite's built-in JSON functions](https://www.sqlite.org/json1.html). For example, this will work with Google's geocoder:
100+
101+
```sql
102+
select
103+
json_extract(raw, '$.formatted_address') as address,
104+
json_extract(raw, '$.geometry.location_type') as location_type
105+
from
106+
innout_test
107+
```
108+
109+
Check each geocoding service's documentation for what's included in the response.
110+
90111
## Python API
91112

92113
The command line interface aims to support the most common options for each geocoder. For more find-grained control, use the Python API.

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup
22
import os
33

4-
VERSION = "0.7.0"
4+
VERSION = "0.8.0"
55

66
requirements = ["click>=7.0", "sqlite_utils", "geopy"]
77

Diff for: tests/test_geocode_sqlite.py

+41
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,44 @@ def test_capture_raw(db, db_path, geocoder):
426426
result = geo_table.get(row["id"])
427427

428428
assert raw == result
429+
430+
431+
def test_capture_raw_custom(db, db_path, geocoder):
432+
table = db[TABLE_NAME]
433+
geo_table = db[GEO_TABLE]
434+
435+
RAW = "raw_custom"
436+
437+
assert "latitude" not in table.columns_dict
438+
assert "longitude" not in table.columns_dict
439+
assert RAW not in table.columns_dict
440+
441+
# run the cli with our test geocoder
442+
runner = CliRunner()
443+
result = runner.invoke(
444+
cli,
445+
[
446+
"test", # geocoder subcommand
447+
str(db_path), # db
448+
str(TABLE_NAME), # table
449+
"--db-path", # path, for test geocoder
450+
str(db_path),
451+
"--location", # location
452+
"{id}",
453+
"--delay", # delay
454+
"0",
455+
"--raw",
456+
RAW, # capture raw output with a custom name
457+
],
458+
)
459+
460+
print(result.stdout)
461+
assert 0 == result.exit_code
462+
463+
for row in table.rows:
464+
assert type(row.get(RAW)) == str
465+
466+
raw = json.loads(row[RAW])
467+
result = geo_table.get(row["id"])
468+
469+
assert raw == result

0 commit comments

Comments
 (0)