debug info to stderr, fix geojson list

This commit is contained in:
vv01f 2019-06-06 12:34:23 +02:00
parent a88f731dde
commit 40fd4a8ce0
Signed by untrusted user who does not match committer: vv01f
GPG Key ID: 02625A16AC1D1FF6
2 changed files with 26 additions and 8 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env sh #!/usr/bin/env sh
for file in $(ls *-csv) ; do for file in $(ls *-csv) ; do
out=$(echo ${file}|cut -d- -f1) out=$(echo ${file}|cut -d- -f1)".geojson"
test -e ${out}".geojson" && { echo "file exists: "${out}", skipping."; continue; } test -e ${out} && { echo "file exists: "${out}", skipping."; continue; }
echo ./lookup.py ${file} echo ./lookup.py ${file}
./lookup.py ${file} > ${out}".geojson" ./lookup.py ${file} 2>/dev/null > ${out}
done done

View File

@ -1,37 +1,55 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from __future__ import print_function
from geopy import Nominatim from geopy import Nominatim
# ~ import json # ~ import json
import sys import sys
import sys
# 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)
# ~ fn="ct-csv" # ~ fn="ct-csv"
if len(sys.argv) == 2: if len(sys.argv) == 2:
fn = sys.argv[1] fn = sys.argv[1]
else: else:
print("in case you do know know what you are doing, better call via e.g. `create-geojson.sh`.\n"); eprint("in case you do know know what you are doing, better call via e.g. `create-geojson.sh`.\n");
print("expecting one argument: csv-file."); eprint("expecting one argument: csv-file.");
print("received "+str(len(sys.argv))+" argument(s): "+str(sys.argv)); eprint("received "+str(len(sys.argv))+" argument(s): "+str(sys.argv));
sys.exit(1) # exit with error sys.exit(1) # exit with error
# for retreiving geo coordinates from addresses # for retreiving geo coordinates from addresses
geolocator = Nominatim(user_agent="my-mapper") geolocator = Nominatim(user_agent="my-mapper")
# lat: 0=111,1km,1=11km,2=1km… lng: 0=70km,1=7km,2=.7km… # lat: 0=111,1km,1=11km,2=1km… lng: 0=70km,1=7km,2=.7km…
precision = 2 precision = 2
# convert csv to geojson
firstline = true
# start json list
print ( "[" )
# get data from file # get data from file
with open( fn, 'r' ) as fp: with open( fn, 'r' ) as fp:
for place in fp: for place in fp:
#error handling: expect #error handling: expect
arrAddress = place.split(",") arrAddress = place.split(",")
strAddress = ",".join(arrAddress[2:5]) strAddress = ",".join(arrAddress[2:5])
print( strAddress ) eprint( "looking up: "+strAddress )
location = geolocator.geocode( strAddress ) location = geolocator.geocode( strAddress )
if location is not None: # its a class if location is not None: # its a class
# todo: ceil coords to hide true location in format string # todo: ceil coords to hide true location in format string
# ~ strCoordPlace = '{:s},{:.6f},{:.6f}'.format( arrAddress[3], location.latitude, location.longitude ) # ~ strCoordPlace = '{:s},{:.6f},{:.6f}'.format( arrAddress[3], location.latitude, location.longitude )
geojson = '{{"type":"Feature","geometry":{{"type":"Point","coordinates":[{:.'+str(precision)+'f},{:.'+str(precision)+'f}]}},"properties":{{"name":"{:s}","marker":"{:s}"}}}}'; geojson = '{{"type":"Feature","geometry":{{"type":"Point","coordinates":[{:.'+str(precision)+'f},{:.'+str(precision)+'f}]}},"properties":{{"name":"{:s}","marker":"{:s}"}}}}';
strCoordPlace = geojson.format( location.latitude, location.longitude, arrAddress[1], fn.split("-")[0] ) strCoordPlace = geojson.format( location.latitude, location.longitude, arrAddress[1], fn.split("-")[0] )
# ~ {"type": "Feature","geometry": {"type": "Point","coordinates":[125.6, 10.1]},"properties":{"name":"Dinagat Islands"}} if firstline == false:
print (",")
else
firstline = false
print ( strCoordPlace ) print ( strCoordPlace )
# end json list
print ( "]" )
# exit fine # exit fine
sys.exit(0) sys.exit(0)