Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Last active December 4, 2023 13:01
Show Gist options
  • Save leplatrem/6294314 to your computer and use it in GitHub Desktop.
Save leplatrem/6294314 to your computer and use it in GitHub Desktop.
Convert list of WMO weather stations to GeoJSON
import csv
import json
def dms2latlng(value):
"""
Degres Minutes Seconds to Decimal degres
"""
degres, minutes, seconds = value.split()
seconds, direction = seconds[:-1], seconds[-1]
dec = float(degres) + float(minutes)/60 + float(seconds)/3600
if direction in ('S', 'W'):
return -dec
return dec
csv_file = 'Pub9volA130819x.flatfile.txt'
geojson_file = csv_file.replace('.txt', '.geojson')
reader = csv.DictReader(open(csv_file, 'rb'), delimiter="\t")
geojson = dict(type='FeatureCollection', features=[])
for line in reader:
lng = dms2latlng(line.pop('Longitude'))
lat = dms2latlng(line.pop('Latitude'))
wmoid = int(line.pop('StationId'))
props = dict(name=line.pop('StationName').title(),
wmoid=wmoid,
model='webmap.WeatherStation')
geom = dict(type='Point',
coordinates=[lng, lat])
feature = dict(type='Feature',
id=wmoid,
geometry=geom,
properties=props)
geojson['features'].append(feature)
json.dump(geojson, open(geojson_file, 'wb'))
@yustiks
Copy link

yustiks commented Dec 4, 2023

Where can I find this file 'Pub9volA130819x.flatfile.txt'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment