ccc-map/lookup.py

72 lines
2.5 KiB
Python
Raw Normal View History

2019-05-21 23:10:20 +02:00
#!/usr/bin/env python3
2019-06-06 12:34:23 +02:00
from __future__ import print_function
2019-05-21 23:10:20 +02:00
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
2020-01-30 23:48:14 +01:00
#disable ssl verification
import ssl
import geopy.geocoders
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
geopy.geocoders.options.default_ssl_context = ctx
#/
2019-06-06 12:34:23 +02:00
# debug print to stderr, https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
2019-06-05 11:40:34 +02:00
# ~ fn="ct-csv"
if len(sys.argv) == 2:
2019-06-06 12:39:15 +02:00
fn = sys.argv[1]
2019-06-05 11:40:34 +02:00
else:
2019-11-21 23:49:54 +01:00
eprint("in case you do not know what you are doing, better call via e.g. `create-geojson.sh`.\n");
2019-06-06 12:39:15 +02:00
eprint("expecting one argument: csv-file.");
eprint("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-06 12:34:23 +02:00
# convert csv to geojson
2019-06-06 12:39:15 +02:00
firstline = True
2019-06-06 12:34:23 +02:00
# start json list
print ( '{"type":"FeatureCollection","features":[' )
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(",")
2019-11-21 23:49:54 +01:00
strAddress = ",".join(arrAddress[2:6])
2019-06-06 12:34:23 +02:00
eprint( "looking up: "+strAddress )
2019-06-05 11:00:53 +02:00
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}"}}}}';
strCoordPlace = geojson.format( location.longitude, location.latitude, arrAddress[1], fn.split("-")[0] )
2019-06-06 12:39:15 +02:00
if firstline == False :
print (",")
else:
firstline = False
2019-06-05 11:00:53 +02:00
print ( strCoordPlace )
2019-06-06 12:34:23 +02:00
# end json list
print ( "]}" )
2019-06-05 11:00:53 +02:00
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