Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from my GeoPython talk
pip install meza
# readers
>>> from meza import io
>>>
>>> records = io.read('kibo_peak.geojson')
>>> next(records)
{'id': 11,
 'lon': Decimal('37.353333'),
 'lat': Decimal('-3.075833'),
 'peak': 'kibo',
 'type': 'Point'}

# merging
>>> from meza import convert as cv
>>>
>>> paths = ('uhuru_peak.geojson', 'kibo_peak.geojson')
>>> records = io.join(*paths)
>>> geojson = cv.records2geojson(records)
>>> io.write('meza_peaks.geojson', geojson)
>>>
>>> # read in the geojson file we just wrote,
>>> # and write it out as a csv file
>>> records = io.read('meza_peaks.geojson')
>>> csv = cv.records2csv(records)
>>> io.write('meza_peaks.csv', csv)

# split by id
>>> from meza import process as pr
>>>
>>> records = io.read('meza_peaks.geojson')
>>> groups = pr.group(records, 'id')
>>> name = 'peak_{}.geojson'
>>>
>>> for _id, _records in groups:
...     f = cv.records2geojson(_records)
...     io.write(name.format(_id), f)

# extract by id
>>> records = io.read('peaks.geojson')
>>> groups = pr.group(records, 'id')
>>> group = next(g for g in groups if g[0] == 11)
>>> geojson = cv.records2csv(group[1])
>>> io.write('id_11_peaks.csv', geojson)

# extract by id v2
>>> from urllib.request import urlopen
>>>
>>> BASE = 'https://raw.githubusercontent.com'
>>> REPO = 'drei01/geojson-world-cities'
>>> path = '{}/{}/master/cities.geojson'
>>> url = path.format(BASE, REPO)
>>> f = urlopen(url)
>>> records = io.read_geojson(f)
>>> next(records)
{'NAME': 'TORSHAVN',
 'id': None,
 'lat': Decimal('62.015167236328125'),
 'lon': Decimal('-6.758638858795166'),
 'pos': 0,
 'type': 'Polygon'}
>>> clean = (r for r in records if r.get('NAME'))
>>> splits = pr.split(clean, 'NAME', chunksize=1024)
>>> b_splits = (s for s in splits if 'BASE' in s[1])
>>> name = 'base_cities.csv'
>>>
>>> for pos, split in enumerate(b_splits):
...     f = cv.records2csv(split[0], skip_header=pos)
...     io.write(name, f, mode='ab+')
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.