Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created June 15, 2011 16:14
Show Gist options
  • Select an option

  • Save tebeka/1027445 to your computer and use it in GitHub Desktop.

Select an option

Save tebeka/1027445 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
data = '''GEOMETRYCOLLECTION (
POINT (-8.9648437500000000 -4.1308593750000000),
POINT (2.0214843750000000 -2.6367187500000000),
POINT (-1.4062500000000000 -11.1621093750000000),
POINT (-11.9531250000000000 -10.8984375000000000),
POLYGON
((-21.6210937500000000 1.8457031250000000,2.4609375000000000
2.1972656250000000, -18.9843750000000000 -3.6914062500000000,
-22.6757812500000000 -3.3398437500000000, -22.1484375000000000
-2.6367187500000000, -21.6210937500000000
1.8457031250000000)),
LINESTRING (-11.9531250000000000
11.3378906250000000, 7.7343750000000000 11.5136718750000000,
12.3046875000000000 2.5488281250000000, 12.2167968750000000
1.6699218750000000, 14.5019531250000000 3.9550781250000000))'''
import re
from collections import defaultdict
from pprint import pprint
prefix = len("GEOMETRYCOLLECTION")
shapes = defaultdict(list)
for match in re.finditer("([A-Z]+)\s*(\(+[^)]+\))+", data[prefix:]):
type = match.group(1)
matches = re.finditer("(-?\d+\.\d+)\s*(-?\d+\.\d+)", match.group(2))
coords = [(float(m.group(1)), float(m.group(2))) for m in matches]
shapes[type].append(coords)
pprint(dict(shapes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment