ccc-map/lookup.py

46 lines
1.8 KiB
Python
Raw Normal View History

2019-05-21 23:10:20 +02:00
#!/usr/bin/env python3
from geopy import Nominatim
2019-06-05 11:00:53 +02:00
# ~ import json
2019-06-05 11:40:34 +02:00
import sys
2019-05-21 23:10:20 +02:00
2019-06-05 11:40:34 +02:00
# ~ fn="ct-csv"
if len(sys.argv) == 2:
fn = sys.argv[1]
else:
print("expecting one argument: csv-file.");
print("received "+str(len(sys.argv))+" argument(s): "+str(sys.argv));
sys.exit(1) # exit with error
2019-05-21 23:10:20 +02:00
2019-06-05 11:40:34 +02:00
# for retreiving geo coordinates from addresses
geolocator = Nominatim(user_agent="my-mapper")
2019-06-06 00:33:26 +02:00
# lat: 0=111,1km,1=11km,2=1km… lng: 0=70km,1=7km,2=.7km…
2019-06-05 12:04:07 +02:00
precision = 2
2019-06-05 11:40:34 +02:00
# get data from file
2019-05-21 23:10:20 +02:00
with open( fn, 'r' ) as fp:
2019-06-05 11:00:53 +02:00
for place in fp:
#error handling: expect
arrAddress = place.split(",")
strAddress = ",".join(arrAddress[1:4])
# ~ print( strAddress )
location = geolocator.geocode( strAddress )
2019-05-21 23:10:20 +02:00
if location is not None: # its a class
2019-06-05 11:00:53 +02:00
# todo: ceil coords to hide true location in format string
# ~ strCoordPlace = '{:s},{:.6f},{:.6f}'.format( arrAddress[3], location.latitude, location.longitude )
2019-06-05 12:30:06 +02:00
geojson = '{{"type":"Feature","geometry":{{"type":"Point","coordinates":[{:.'+str(precision)+'f},{:.'+str(precision)+'f}]}},"properties":{{"name":"{:s}","marker":"{:s}"}}}}';
2019-06-05 12:04:07 +02:00
strCoordPlace = geojson.format( location.latitude, location.longitude, arrAddress[3], fn.split("-")[0] )
2019-06-05 11:00:53 +02:00
# ~ {"type": "Feature","geometry": {"type": "Point","coordinates":[125.6, 10.1]},"properties":{"name":"Dinagat Islands"}}
print ( strCoordPlace )
2019-06-05 11:40:34 +02:00
# exit fine
sys.exit(0)
2019-06-05 11:00:53 +02:00
# ~ m = folium.Map( # etc..)
# ~ m.save("filename.png")
# ~ https://github.com/python-visualization/folium/issues/35#issuecomment-164784086
# ~ https://github.com/wbkd/leaflet-mapshot
# ~ https://stackoverflow.com/questions/44800396/python-ipyleaflet-export-map-as-png-or-jpg-or-svg
# ~ m = folium.Map(location=[51, 13], zoom_start=5, tiles='Stamen Toner') # Mapbox Bright , CartoDB Positron