-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_parcel_centroid.py
executable file
·124 lines (87 loc) · 3.67 KB
/
init_parcel_centroid.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!./env/bin/python
"""
Given a parcel lines shapefile downloaded from
http://www.mcauditor.org/downloads/gis_download_shape.cfm,
that has been converted from Lambert_Conformal_Conic to D_WGS_1984 via
the following command:
ogr2ogr -t_srs EPSG:4326 out.shp in.shp
this Python script creates a centroids.dbm file that contains the following
(key,value) pairs:
key: Shapefile record 'taxpinno'
value: Shapefile shape bounding box centroid (latitude, longitude)"""
import dbm
import numpy as np
import re
import shapefile
import sys
def initialize_record(fields, shape_record):
"""Initializes a dictionary that stores a shapefile record.
Args:
fields: List that stores the shapefile record names
shape_record: Object that contains a shapefile record
and associated shape
Returns:
record: Dictionary that stores a shapefile record."""
record = {}
for idx in range(0, len(fields)):
record[fields[idx]] = shape_record.record[idx]
if isinstance(record[fields[idx]], str):
record[fields[idx]] =\
re.sub("\\s+", " ", record[fields[idx]])
return record
def is_government(name):
"""Returns a boolean that keeps track of whether or not a shapefile record
is associated with a local, county, or state government.
Args:
name: String that stores a shapefile record name
Returns:
is_government_flag: Boolean that keeps track of whether or not a
shapefile record is associated with a local
county, or state government."""
is_government_flag = False
government_regex = ['^city of dayton.*$',
'^board of county commissioners$',
'^dayton mont co public$',
'^dayton, city of$',
'^miami conservancy district$',
'^state of ohio.*$']
for regex in government_regex:
is_government_flag =\
is_government_flag | (re.match(regex, name) is not None)
return is_government_flag
def init_parcel_centroid(argv):
"""Given a parcel lines shapefile downloaded from
http://www.mcauditor.org/downloads/gis_download_shape.cfm,
that has been converted from Lambert_Conformal_Conic to D_WGS_1984 via
the following command:
ogr2ogr -t_srs EPSG:4326 out.shp in.shp
this Python script creates a centroids.dbm file that contains the
following (key,value) pairs:
key: Shapefile record 'taxpinno'
value: Shapefile shape bounding box centroid (latitude, longitude)
Args:
argv: List that stores command line arguments
Index: Description:
----- -----------
0 Python script name
1 Parcel lines shapefile basename
Returns:
None"""
shapefileobj = shapefile.Reader(argv[1])
fields = [re.sub("_", "", field[0].lower())
for field in shapefileobj.fields[1:]]
centroids = dbm.open("centroids.dbm", "c")
for shape_record in shapefileobj.shapeRecords():
record = initialize_record(fields, shape_record)
if (shape_record.shape and shape_record.shape.shapeType != 0):
bbox = np.array(shape_record.shape.bbox)
parcelid = record['taxpinno'].replace(" ", "")
centroids[parcelid] =\
"%f %f %s" % (np.mean(bbox[1:4:2]),
np.mean(bbox[0:4:2]), record['locarea'])
centroids.close()
if __name__ == "__main__":
if len(sys.argv) == 2:
init_parcel_centroid(sys.argv)
else:
print("Usage: init_parcel_centroid.py <parcellines shapefile name>")