Skip to content

Instantly share code, notes, and snippets.

@subdavis
Last active October 1, 2024 23:47
Show Gist options
  • Save subdavis/26bcec56f351eee165237766ee72eca3 to your computer and use it in GitHub Desktop.
Save subdavis/26bcec56f351eee165237766ee72eca3 to your computer and use it in GitHub Desktop.
import requests
import csv
import json
# This library has some handy stuff for working with geographic data
# import geopy.distance
# A google API token with geocoder service enabled
api_key="CHANGEME"
# Cache Geocoder api responses
geocode_cache = {}
with open('geocode_cache.json') as f:
geocode_cache = json.load(f)
# Dump the cache to file.
def save_geocode_cache_json():
with open('geocode_cache.json', 'w') as f:
json.dump(geocode_cache, f)
# If you happen to want to use the google geocoder api to transform addresses into latlng
# Not relevent to the CSV parsing bits
def get_lat_lng(address):
base_url = 'https://maps.googleapis.com/maps/api/geocode/json'
params = {
'address': address,
'key': api_key
}
if address in geocode_cache:
return geocode_cache[address]
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
if data['status'] == 'OK':
print(f"Geocoded {address} to {data['results'][0]['geometry']['location']}")
location = data['results'][0]['geometry']['location']
geocode_cache[address] = [location['lat'], location['lng']]
return [location['lat'], location['lng']]
else:
print(f"Error: {data['status']}")
return None
else:
print(f"HTTP Error: {response.status_code}")
return None
# it works but it's not pretty.
# Works on export sheets with 3 columns like yours seem to have.
# I didn't bother to make it arbitrary.
def parse_yance_map(filename):
with open(filename) as csvfile:
reader = csv.reader(csvfile)
real_lines = []
for row in reader:
# The problem is that notes hang onto one or more newlines
# And the google mymaps exporter doesn't escape or quote correctly
# That's really all this is doing
if row and row[0] and row[0].startswith('POINT'):
real_lines.append(row)
elif row and len(real_lines):
if len(real_lines[-1]) >= 3:
real_lines[-1][2] += f" {row[0]}"
else:
real_lines[-1].append(row[0])
return real_lines
with open('mitchell_geocoded.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['WKT', 'Title', 'Description'])
# Where yancey-info.csv is an exported CSV of one of the layers of your map
rows = parse_yance_map('yancey-info.csv')
writer.writerows(rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment