Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def distance(shape, other):
if not hasattr(shape,'__geo_interface__'): raise TypeError("%r does not appear to be a shape"%shape)
if not hasattr(other,'__geo_interface__'): raise TypeError("%r does not appear to be a shape"%shape)
o = geom.asShape(shape)
o2 = geom.asShape(other)
return o.distance(o2)
for address in input:
shape = asShape(address['geometry'])
shape.original = address
addresses.append(shape)
geoid = re.match('^.*-(\d+)\.shp$', buildingIn).groups(0)[0]
# print "loaded", len(addresses), "addresses"
# Load and index all buildings.
buildings = []
buildingShapes = []
buildingIdx = index.Index()
with collection(buildingIn, "r") as input:
for building in input:
shape = asShape(building['geometry'])
building['properties']['addresses'] = []
buildings.append(building)
buildingShapes.append(shape)
buildingIdx.add(len(buildings) - 1, shape.bounds)
# print "loaded", len(buildings), "buildings"
addressIntersections = {}
addressesOnBuildings = 0
# Map addresses to buildings.
# Note, if there are multiple address points within a building, this
# adds each one as an array
for address in addresses:
features = json.load(f)
allAddresses = {}
buildings = []
buildingShapes = []
buildingIdx = index.Index()
# Returns the coordinates for this address
def keyFromAddress(address):
return str(address['geometry']['coordinates'][0]) + "," + str(address['geometry']['coordinates'][1])
for feature in features:
if feature['geometry']['type'] == 'Polygon' or feature['geometry']['type'] == 'MultiPolygon':
extra_tags = osm_tags.get_osm_tags(feature)
feature['properties']['osm'] = extra_tags
buildings.append(feature)
shape = asShape(feature['geometry'])
buildingShapes.append(shape)
buildingIdx.add(len(buildingShapes) - 1, shape.bounds)
# These are the addresses that don't overlap any buildings
elif feature['geometry']['type'] == 'Point':
# The key is the coordinates of this address. Track how many addresses share these coords.
key = keyFromAddress(feature)
if key in allAddresses:
allAddresses[key].append(feature)
else:
allAddresses[key] = [feature]
else:
print "geometry of unknown type:", feature['geometry']['type']
# Generates a new osm id.
def is_ring(shape):
if not hasattr(shape,'__geo_interface__'): raise TypeError("%r does not appear to be a shape"%shape)
o = geom.asShape(shape)
return o.is_ring
def run(self):
while True:
try:
message = self.request_queue.get()
shape = asShape(message["shape"])
key = message["key"]
point = message["point"]
if shape.intersects(point):
self.response_queue.put(key)
self.request_queue.task_done()
except Queue.Empty:
continue
def distance(shape, other):
if not hasattr(shape,'__geo_interface__'): raise TypeError("%r does not appear to be a shape"%shape)
if not hasattr(other,'__geo_interface__'): raise TypeError("%r does not appear to be a shape"%shape)
o = geom.asShape(shape)
o2 = geom.asShape(other)
return o.distance(o2)
def is_empty(shape):
if not hasattr(shape,'__geo_interface__'): raise TypeError, "%r does not appear to be a shape"%shape
o = shapely.geometry.asShape(shape)
return o.is_empty
def shape_poly(self):
with fiona.open(self.boundary) as fiona_collection:
# In this case, we'll assume the shapefile only has one later
shapefile_record = fiona_collection.next()
# Use Shapely to create the polygon
self.polygon = geometry.asShape( shapefile_record['geometry'] )
return self.polygon
def equals_exact(shape, other, tolerance):
if not hasattr(shape,'__geo_interface__'): raise TypeError, "%r does not appear to be a shape"%shape
if not hasattr(other,'__geo_interface__'): raise TypeError, "%r does not appear to be a shape"%shape
o = shapely.geometry.asShape(shape)
o2 = shapely.geometry.asShape(other)
return o.equals_exact(o2, tolerance)
''' Decode a GeoJSON file into a list of (WKB, property dict) features.
Result can be passed directly to mapnik.PythonDatasource.wkb_features().
'''
data = json.load(file)
features = []
for feature in data['features']:
if feature['type'] != 'Feature':
continue
if feature['geometry']['type'] == 'GeometryCollection':
continue
prop = feature['properties']
geom = transform(asShape(feature['geometry']), mercator)
features.append((geom.wkb, prop))
return features