forked from civicdata/louisville-buildings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.py
68 lines (56 loc) · 2.47 KB
/
chunk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# chunk.py
# Exports intersections between two shapefile's objects as separate files.
from fiona import collection
from rtree import index
from shapely.geometry import asShape
from shapely import speedups
from sys import argv
from pprint import pprint
speedups.enable()
# Exports given features into shapefiles by sections
def chunk(featureFileName, sectionFileName, pattern, key = None):
# Load and index
with collection(featureFileName, "r") as featureFile:
featureIdx = index.Index()
features = []
for feature in featureFile:
features.append(feature)
featureIdx.add(len(features) - 1, asShape(feature['geometry']).bounds)
# Break up by sections and export
with collection(sectionFileName, "r") as sectionFile:
i = 0
for section in sectionFile:
fileName = pattern % i
if key:
fileName = pattern % section['properties'][key][-7:] # use only last 7 chars of key for legibility
properties = {}
try:
with collection(fileName, 'w', 'ESRI Shapefile',
schema = featureFile.schema,
crs = featureFile.crs) as output:
sectionShape = asShape(section['geometry'])
for j in featureIdx.intersection(sectionShape.bounds):
if asShape(features[j]['geometry']).intersects(sectionShape):
output.write(features[j])
print "Exported %s" % fileName
i = i + 1
except ValueError:
print 'Error exporting ' + fileName
pprint(properties)
pprint(featureFile.schema)
usage = """
chunk.py
========
Exports intersections between two shapefile's objects as separate files.
Usage: python chunk.py featuresfile sectionfile outputfilepattern [attributekey]
Example:
python chunk.py manysmallfeatures.shp sections.shp export/features-by-section-%s.shp
If you specify an attribute key that can be found in the sections file it will be used for naming output files:
python chunk.py manysmallfeatures.shp sections.shp export/features-by-section-%s.shp STATECODE
"""
if len(argv) == 4:
chunk(argv[1], argv[2], argv[3])
elif len(argv) == 5:
chunk(argv[1], argv[2], argv[3], argv[4])
else:
print usage