def test_collection(self): # Empty self.assertSVG(GeometryCollection(), '<g />') # Valid self.assertSVG( Point(7, 3).union(LineString([(4, 2), (8, 4)])), '<g><circle cx="7.0" cy="3.0" r="3.0" stroke="#555555" ' 'stroke-width="1.0" fill="#66cc99" opacity="0.6" />' '<polyline fill="none" stroke="#66cc99" stroke-width="2.0" ' 'points="4.0,2.0 8.0,4.0" opacity="0.8" /></g>') # Invalid self.assertSVG( Point(7, 3).union(LineString([(4, 2), (4, 2)])), '<g><circle cx="7.0" cy="3.0" r="3.0" stroke="#555555" ' 'stroke-width="1.0" fill="#ff3333" opacity="0.6" />' '<polyline fill="none" stroke="#ff3333" stroke-width="2.0" ' 'points="4.0,2.0 4.0,2.0" opacity="0.8" /></g>') # Colorful self.assertSVG( GeometryCollection([ Point(7, 3), LineString([(4, 2), (4, 2)]), ], colors=['red', 'blue']), '<g><circle cx="7.0" cy="3.0" r="3.0" stroke="#555555" ' 'stroke-width="1.0" fill="red" opacity="0.6" />' '<polyline fill="none" stroke="blue" stroke-width="2.0" ' 'points="4.0,2.0 4.0,2.0" opacity="0.8" /></g>')
def test_geo_op_empty_result(self): l1 = LineString([(0, 0), (1, 1)]) l2 = LineString([(2, 2), (3, 3)]) expected = GeoSeries([GeometryCollection()]) # binary geo resulting in empty geometry result = GeoSeries([l1]).intersection(l2) assert_geoseries_equal(result, expected) # binary geo empty result with right GeoSeries result = GeoSeries([l1]).intersection(GeoSeries([l2])) assert_geoseries_equal(result, expected) # unary geo resulting in emtpy geometry result = GeoSeries([GeometryCollection()]).convex_hull assert_geoseries_equal(result, expected)
def test_geometrycollection_in_geometrycollection(): coll = GeometryCollection([p, geom, ring]) coll2 = GeometryCollection([coll, p, geom, ring]) coll2_proto = topb.serialize(coll2) deserialized2 = to_shapely.deserialize(coll2_proto) assert type(deserialized2) is GeometryCollection assert deserialized2.equals(coll2)
def render_buildings(x1, y1, x2, y2): cursor = db.cursor() # fetch ground polygons, they will be extruded on the client cursor.execute( """ SELECT geometry FROM osm_buildings WHERE geometry && ST_Transform(ST_MakeEnvelope(%s, %s, %s, %s, 4326), 3857) """, (x1, y1, x2, y2)) p = Proj(init='EPSG:3857') px, py = p(x1, y2) px1, py1 = p(x1, y1) px2, py2 = p(x2, y2) bounding = box(px1, py1, px2, py2) # load the geometry, intersect with bounding box and translate to origin # to be able to tile load in the client later on polys = [] for item in cursor: geo = loads(item['geometry'], hex=True).intersection(bounding) geo = translate(geo, -px, -py, 0) polys.append(geo) # join everything into a collection and dump WKB to the response collection = GeometryCollection(polys) response = make_response(dumps(collection), 200) response.headers['Content-Type'] = 'application/octet-stream' return response
def test_line_inside_collection_of_points_does_not_intersect(self): geom1 = LineString([(0.5, 0), (0.5, 1)]) geom2 = GeometryCollection( [Point(0, 0), Point(0, 1), Point(1, 0), Point(1, 1)]) intersects = common.intersects(geom1, geom2) self.assertEqual(intersects, False)
def add_population(df_final): """ Adds the population data to the final data set. Only meant to be called after fix_polygons(). """ def get_population_density(row): if row['geometry'].area == 0 or row['geometry'] is None: return 0 else: # get population / area and convert to km^2 return row['2019'] / row['geometry'].area * 10**6 df_pop = pd.read_csv( "../database/API_SP.POP.TOTL_DS2_en_csv_v2_1976634.csv", header=2) df_final = df_final.merge(df_pop[['Country Code', 'Country Name', '2019']], how='outer', left_on='ISO_A3', right_on='Country Code') # change any None geometries to actual geometry objects... df_copy = df_final['geometry'].apply(lambda x: x if x else GeometryCollection()) # ...so next line doesn't throw error # change to CRS which gives most accurate area values df_copy = df_copy.to_crs({'proj': 'cea'}) df_final['pop_density'] = df_final['2019'] / df_copy.area * 10**6 # df_final['geometry'] = df_final['geometry'].to_crs(epsg=3857) # change to CRS back to longitude/latitude return df_final
def test_numpy_object_array(): np = pytest.importorskip("numpy") geom = GeometryCollection([LineString([(0, 0), (1, 1)])]) ar = np.empty(1, object) ar[:] = [geom] assert ar[0] == geom
def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological("difference", expected, self.g1, self.g2) with pytest.warns(UserWarning, match="The indices .+ different"): assert len(self.g0.difference(self.g9, align=True) == 8) assert len(self.g0.difference(self.g9, align=False) == 7)
def deserialize_geometry_collection(self, gpbGeometry) -> GeometryCollection: geometries = [ self.deserialize_geometry(geometry) for geometry in gpbGeometry.geometries ] return GeometryCollection(geometries)
def test_buffer_empty_none(self): p = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) s = GeoSeries([p, GeometryCollection(), None]) result = s.buffer(0) assert_geoseries_equal(result, s) result = s.buffer(np.array([0, 0, 0])) assert_geoseries_equal(result, s)
def test_collection_two_line_targets(self): geom = GeometryCollection([Point(0, 0), LineString([(1, 0), (1, 1)])]) targets = [ LineString([(-0.5, 0), (0.5, 0)]), LineString([(1.1, 0), (1.1, 1)]) ] closest_geometry = common.closest(geom, targets, 1) self.assertEqual(closest_geometry, LineString([(-0.5, 0), (0.5, 0)]))
def getShapelyGeometry(feature): if not feature["geometry"]: return None elif feature["geometry"]["type"] == "GeometryCollection": return GeometryCollection( [shape(g) for g in feature["geometry"]["geometries"]]) else: return shape(feature["geometry"])
def render_streets(x1, y1, x2, y2): cursor = db.cursor() # types of roads to load and their width in meters typ_buf = [ # ( ('pedestrian', 'footway', 'path'), 3), (('residential', 'living_street', 'unclassified', 'road'), 8.5), (('tertiary', 'tertiary_link'), 9.5), (('secondary', 'secondary_link', 'trunk', 'trunk_link'), 15.5), (('primary', 'primary_link'), 20), (('motorway', 'motorway_link'), 30), ] # we need to project some values into the DB projection p = Proj(init='EPSG:3857') px1, py1 = p(x1, y1) px2, py2 = p(x2, y2) bounding = box(px1, py1, px2, py2) px, py = p(x1, y2) # fetch street geometry result_polys = [] for types, width in typ_buf: cursor.execute( """ SELECT * FROM osm_roads WHERE geometry && ST_Transform(ST_MakeEnvelope(%s, %s, %s, %s, 4326), 3857) AND ({tp}) """.format(tp=" OR ".join([f'"type" = \'{n}\'' for n in types])), (x1, y1, x2, y2)) polys = [] for item in cursor: geo = loads(item['geometry'], hex=True).buffer(width / 2.0, resolution=2, join_style=2) polys.append(geo) # fuse all street polygons of this layer to avoid flicker and intersect with bounding box poly = cascaded_union(polys).intersection(bounding) # now translate everything so it starts at 0,0 (to be able to tile-load data later) poly = translate(poly, -px, -py, 0) result_polys.append(poly) # nothing found -> 404 if len(result_polys) == 0: abort(404) # combine everything into a collection and dump WKB to the response collection = GeometryCollection(result_polys) response = make_response(dumps(collection), 200) response.headers['Content-Type'] = 'application/octet-stream' return response
def geometries_internal_intersection(polygons): """Get the intersection geometries between all geometries in a set. Arguments --------- polygons : `list`-like A `list`-like containing geometries. These will be placed in a :class:`geopandas.GeoSeries` object to take advantage of `rtree` spatial indexing. Returns ------- intersect_list A `list` of geometric intersections between polygons in `polygons`, in the same CRS as the input. """ # convert `polygons` to geoseries and get spatialindex # TODO: Implement test to see if `polygon` items are actual polygons or # WKT strings if isinstance(polygons, gpd.GeoSeries): gs = polygons else: gs = gpd.GeoSeries(polygons).reset_index(drop=True) sindex = gs.sindex gs_bboxes = gs.apply(lambda x: x.bounds) # find indices of polygons that overlap in gs intersect_lists = gs_bboxes.apply(lambda x: list(sindex.intersection(x))) intersect_lists = intersect_lists.dropna() # drop all objects that only have self-intersects # first, filter down to the ones that have _some_ intersection with others intersect_lists = intersect_lists[ intersect_lists.apply(lambda x: len(x) > 1)] if len(intersect_lists) == 0: # if there are no real intersections return GeometryCollection() # same result as failed union below # the below is a royal pain to follow. what it does is create a dataframe # with two columns: 'gs_idx' and 'intersectors'. 'gs_idx' corresponds to # a polygon's original index in gs, and 'intersectors' gives a list of # gs indices for polygons that intersect with its bbox. intersect_lists.name = 'intersectors' intersect_lists.index.name = 'gs_idx' intersect_lists = intersect_lists.reset_index() # first, we get rid of self-intersection indices in 'intersectors': intersect_lists['intersectors'] = intersect_lists.apply( lambda x: [i for i in x['intersectors'] if i != x['gs_idx']], axis=1) # for each row, we next create a union of the polygons in 'intersectors', # and find the intersection of that with the polygon at gs[gs_idx]. this # (Multi)Polygon output corresponds to all of the intersections for the # polygon at gs[gs_idx]. we add that to a list of intersections stored in # output_polys. output_polys = [] _ = intersect_lists.apply(lambda x: output_polys.append( gs[x['gs_idx']].intersection(cascaded_union(gs[x['intersectors']])) ), axis=1) # we then generate the union of all of these intersections and return it. return cascaded_union(output_polys)
def test_geometrycollection_serialization(): coll = GeometryCollection([p, geom, ring]) coll_proto = topb.serialize(coll) deserialized = to_shapely.deserialize(coll_proto) assert type(deserialized) is GeometryCollection assert deserialized.equals(coll)
def project_feature(f, project): if isinstance(f['geometry'], Polygon): geom = Polygon(f['geometry']) elif isinstance(f['geometry'], MultiPolygon): geom = MultiPolygon(f['geometry']) elif isinstance(f['geometry'], GeometryCollection): geom = GeometryCollection(f['geometry']) elif isinstance(f['geometry'], Point): geom = Point(f['geometry']) projected_geom = transform(project, geom) new_feat = dict(properties=f['properties'], geometry=projected_geom, type='Feature') return new_feat
def project_features(featureset, project): new_features = [] for f in featureset['features']: if isinstance(f['geometry'], Polygon): geom = Polygon(f['geometry']) elif isinstance(f['geometry'], MultiPolygon): geom = MultiPolygon(f['geometry']) elif isinstance(f['geometry'], GeometryCollection): geom = GeometryCollection(f['geometry']) projected_geom = transform(project, geom) new_feat = dict(properties=f['properties'], geometry=projected_geom, type='Feature') new_features.append(new_feat) return dict(type=featureset['type'], features=new_features)
def test_array_interface(self): m = GeometryCollection() self.assertEqual(len(m), 0) self.assertEqual(m.geoms, [])
def test_collection_two_targets(self): geom = GeometryCollection([Point(0, 0), LineString([(1, 0), (1, 1)])]) targets = [Point(0, 0.5), Point(1.4, 0.5)] closest_geometry = common.closest(geom, targets, 1) self.assertEqual(closest_geometry, Point(1.4, 0.5))
def geometrycollection_geojson(): return { "type": "GeometryCollection", "geometries": [{ "type": "Point", "coordinates": (0, 3, 0) }, { "type": "LineString", "coordinates": ((2, 0), (1, 0)) }] } @pytest.mark.parametrize('geom', [ GeometryCollection(), shape({ "type": "GeometryCollection", "geometries": [] }), shape({ "type": "GeometryCollection", "geometries": [{ "type": "Point", "coordinates": () }, { "type": "LineString", "coordinates": (()) }] }),
def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.g1, self.g2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.gdf1, self.g2)
def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological("difference", expected, self.g1, self.g2)
from functools import partial import numpy as np from pandas import Series, DataFrame import pyproj from shapely.geometry import shape, Polygon, Point from shapely.geometry.collection import GeometryCollection from shapely.geometry.base import BaseGeometry from shapely.ops import cascaded_union, unary_union, transform import fiona from fiona.crs import from_epsg from plotting import plot_series EMPTY_COLLECTION = GeometryCollection() EMPTY_POLYGON = Polygon() EMPTY_POINT = Point() def _is_empty(x): try: return x.is_empty except: return False def _is_geometry(x): return isinstance(x, BaseGeometry)
def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological("symmetric_difference", expected, self.g3, self.t1)
def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological("difference", expected, self.g1, self.g2) assert len(self.g0.difference(self.g9, align=True) == 8) assert len(self.g0.difference(self.g9, align=False) == 7)
def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.tz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3)]) self.tz1 = Polygon([(2, 2, 2), (1, 1, 1), (3, 3, 3)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.sqz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)]) self.t4 = Polygon([(0, 0), (3, 0), (3, 3), (0, 2)]) self.t5 = Polygon([(2, 0), (3, 0), (3, 3), (2, 3)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.p3d = Point(5, 5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0, None, ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.gz = GeoSeries([self.tz, self.sqz, self.tz1]) self.g3.crs = "epsg:4326" self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = "epsg:4326" self.g_3d = GeoSeries([self.p0, self.p3d]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ["A", "B"] self.a2 = self.g2.copy() self.a2.index = ["B", "C"] self.esb = Point(-73.9847, 40.7484, 30.3244) self.sol = Point(-74.0446, 40.6893, 31.2344) self.landmarks = GeoSeries([self.esb, self.sol], crs="epsg:4326") self.pt2d = Point(-73.9847, 40.7484) self.landmarks_mixed = GeoSeries([self.esb, self.sol, self.pt2d], crs=4326) self.pt_empty = wkt.loads("POINT EMPTY") self.landmarks_mixed_empty = GeoSeries( [self.esb, self.sol, self.pt2d, self.pt_empty], crs=4326) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) self.g7 = GeoSeries([self.sq, self.t4]) self.g8 = GeoSeries([self.t1, self.t5]) self.empty = GeoSeries([]) self.all_none = GeoSeries([None, None]) self.all_geometry_collection_empty = GeoSeries( [GeometryCollection([]), GeometryCollection([])]) self.empty_poly = Polygon() self.g9 = GeoSeries(self.g0, index=range(1, 8)) self.g10 = GeoSeries([self.t1, self.t4]) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ "geometry": self.g1, "col0": [1.0, 2.0], "col1": ["geo", "pandas"] }) self.gdf2 = GeoDataFrame({ "geometry": self.g1, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdf3 = GeoDataFrame({ "geometry": self.g3, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdfz = GeoDataFrame({ "geometry": self.gz, "col3": [4, 5, 6], "col4": ["rand", "string", "geo"] })
def HandlePlacemark(self, placeName, shape, extendedData): #print placeName, shape #print extendedData externalId = extendedData["ListEntry"] del extendedData["ListEntry"] del extendedData["NGR"] del extendedData["Easting"] del extendedData["Northing"] del extendedData["AREA_HA"] rec = DatasetRecord(externalId=externalId, dataJson=json.dumps(extendedData), datasetSnapshot=self.snapshot) rec.save() if not shape.is_valid: shape = shape.buffer(0.0) rp = shape.representative_point() #Create main record if it does not already exist try: rec2 = Record.objects.get(externalId=externalId) except dex.ObjectDoesNotExist: pos = GEOSGeometry(str(rp), srid=4326) rec2 = Record(currentName=placeName, currentPosition=pos, datasetSeries=self.ds, externalId=externalId) rec2.save() newAnnot = RecordNameEdit(record=rec2, data=placeName, timestamp=self.importTime, user=self.importUser) newAnnot.save() newAnnot = RecordPositionEdit(record=rec2, data=pos, timestamp=self.importTime, user=self.importUser) newAnnot.save() #Clear any old shape annotations if True: chkShps = RecordShapeEdit.objects.filter(record=rec2) for shp in chkShps: shp.delete() #Replace with new shape if True: #Can't do this on spatialite for points near zero #https://code.djangoproject.com/ticket/27672 shape2 = GeometryCollection([shape]) shape3 = GEOSGeometry(buffer(shape2.wkb), srid=4326) newAnnot = RecordShapeEdit(record=rec2, data=shape3, timestamp=self.importTime, user=self.importUser) newAnnot.save()
def test_line_inside_mixed_collection_does_intersect(self): geom1 = LineString([(0.5, 0), (0.5, 1)]) geom2 = GeometryCollection( [Point(0, 0), LinearRing([(0, 1), (1, 0), (1, 1)])]) intersects = common.intersects(geom1, geom2) self.assertEqual(intersects, True)
def test_array_interface(self): m = GeometryCollection() self.failUnlessEqual(len(m), 0) self.failUnlessEqual(m.geoms, [])
def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_operator('__sub__', expected, self.g1, self.g2)