def city_from_extent(extent): city_extents = dict([(m['short_name'], m['extent']) for m in METRO_LIST]) env = OGRGeometry(Envelope(*extent).wkt) matches = [] for slug, city_ext in city_extents.iteritems(): city_env = OGRGeometry(Envelope(*city_ext).wkt) if city_env.intersects(env): matches.append((slug, city_env)) if len(matches) == 1: return matches[0][0] elif len(matches) > 1: # Crudely select the intersecting city with the most overlap # TODO: get rid of this current_best_slug, current_max_area = None, float('-inf') for slug, city_env in matches: intersection = city_env.intersection(env) area = intersection.area if area > current_max_area: current_max_area = area current_best_slug = slug return current_best_slug # If we didn't find a match with a city extent, start expanding the buffer # around the city extents until we match one for i in xrange(6): for slug, city_ext in city_extents.iteritems(): extent = buffer_extent(city_ext, 1, num_tiles=i+1) city_env = OGRGeometry(Envelope(*extent).wkt) if env.intersects(city_env): return slug
def city_from_extent(extent): city_extents = dict([(m['short_name'], m['extent']) for m in METRO_LIST]) env = OGRGeometry(Envelope(*extent).wkt) matches = [] for slug, city_ext in city_extents.iteritems(): city_env = OGRGeometry(Envelope(*city_ext).wkt) if city_env.intersects(env): matches.append((slug, city_env)) if len(matches) == 1: return matches[0][0] elif len(matches) > 1: # Crudely select the intersecting city with the most overlap # TODO: get rid of this current_best_slug, current_max_area = None, float('-inf') for slug, city_env in matches: intersection = city_env.intersection(env) area = intersection.area if area > current_max_area: current_max_area = area current_best_slug = slug return current_best_slug # If we didn't find a match with a city extent, start expanding the buffer # around the city extents until we match one for i in xrange(6): for slug, city_ext in city_extents.iteritems(): extent = buffer_extent(city_ext, 1, num_tiles=i + 1) city_env = OGRGeometry(Envelope(*extent).wkt) if env.intersects(city_env): return slug
def test_intersection(self): "Testing intersects() and intersection()." for i in range(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt) self.assertTrue(a.intersects(b)) i2 = a.intersection(b) self.assertEqual(i1, i2) self.assertEqual(i1, a & b) # __and__ is intersection operator a &= b # testing __iand__ self.assertEqual(i1, a)
def test11_intersection(self): "Testing intersects() and intersection()." for i in xrange(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt) self.assertEqual(True, a.intersects(b)) i2 = a.intersection(b) self.assertEqual(i1, i2) self.assertEqual(i1, a & b) # __and__ is intersection operator a &= b # testing __iand__ self.assertEqual(i1, a)
def test11_intersection(self): "Testing intersects() and intersection()." for i in xrange(len(topology_geoms)): g_tup = topology_geoms[i] a = OGRGeometry(g_tup[0].wkt) b = OGRGeometry(g_tup[1].wkt) i1 = OGRGeometry(intersect_geoms[i].wkt) self.assertEqual(True, a.intersects(b)) i2 = a.intersection(b) self.assertEqual(i1, i2) self.assertEqual(i1, a & b) # __and__ is intersection operator a &= b # testing __iand__ self.assertEqual(i1, a)
def test_intersection(self): "Testing intersects() and intersection()." for i in range(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt) self.assertTrue(a.intersects(b)) i2 = a.intersection(b) self.assertTrue(i1.geos.equals(i2.geos)) self.assertTrue(i1.geos.equals( (a & b).geos)) # __and__ is intersection operator a &= b # testing __iand__ self.assertTrue(i1.geos.equals(a.geos))
def read_node(f, filter_env, ids): """ Follows query_node() in plugins/input/shape/shp_index.hpp """ (offset,) = struct.unpack('i', f.read(4)) envelope = OGRGeometry(Envelope(*struct.unpack('4d', f.read(32))).wkt) (shape_count,) = struct.unpack('i', f.read(4)) if not envelope.intersects(filter_env): f.seek(offset + shape_count * 4 + 4, 1) return ids.extend(struct.unpack('%di' % shape_count, f.read(4 * shape_count))) (num_children,) = struct.unpack('i', f.read(4)) for i in xrange(num_children): read_node(f, filter_env, ids)
def pixel_value_from_point(raster, point, band=0): """ Returns the pixel value for the coordinate of the input point from selected band. The input can be a point or tuple, if its a tuple it is assumed to be coordinates in the reference system of the raster. """ if isinstance(point, (tuple, list)): point = OGRGeometry('POINT({0} {1})'.format(*point)) point.srid = raster.srid elif not point.srs or not raster.srs: raise ValueError( 'Both the point and the raster are required to have a reference system specified.' ) elif point.srs != raster.srs: # Ensure the projection of the point is the same as of the raster. point.transform(raster.srid) # Return if point and raster do not touch. bbox = OGRGeometry.from_bbox(raster.extent) bbox.srs = raster.srs if not point.intersects(bbox): return # Compute position of point relative to raster origin. offset = (abs(raster.origin.x - point.coords[0]), abs(raster.origin.y - point.coords[1])) # Compute pixel index value based on offset. offset_index = [ int(offset[0] / abs(raster.scale.x)), int(offset[1] / abs(raster.scale.y)) ] # If the point is exactly on the boundary, the offset_index is rounded to # a pixel index over the edge of the pixel. The index needs to be reduced # by one pixel for those cases. if offset_index[0] == raster.width: offset_index[0] -= 1 if offset_index[1] == raster.height: offset_index[1] -= 1 return raster.bands[band].data(offset=offset_index, size=(1, 1))[0, 0]
def pixel_value_from_point(raster, point, band=0): """ Returns the pixel value for the coordinate of the input point from selected band. The input can be a point or tuple, if its a tuple it is assumed to be coordinates in the reference system of the raster. """ if isinstance(point, (tuple, list)): point = OGRGeometry('POINT({0} {1})'.format(*point)) point.srid = raster.srid elif not point.srs or not raster.srs: raise ValueError('Both the point and the raster are required to have a reference system specified.') elif point.srs != raster.srs: # Ensure the projection of the point is the same as of the raster. point.transform(raster.srid) # Return if point and raster do not touch. bbox = OGRGeometry.from_bbox(raster.extent) bbox.srs = raster.srs if not point.intersects(bbox): return # Compute position of point relative to raster origin. offset = (abs(raster.origin.x - point.coords[0]), abs(raster.origin.y - point.coords[1])) # Compute pixel index value based on offset. offset_index = [int(offset[0] / abs(raster.scale.x)), int(offset[1] / abs(raster.scale.y))] # If the point is exactly on the boundary, the offset_index is rounded to # a pixel index over the edge of the pixel. The index needs to be reduced # by one pixel for those cases. if offset_index[0] == raster.width: offset_index[0] -= 1 if offset_index[1] == raster.height: offset_index[1] -= 1 return raster.bands[band].data(offset=offset_index, size=(1, 1))[0, 0]