def test_from_shape(): p = Point(1, 2) e = from_shape(p) assert isinstance(e, WKBElement) assert isinstance(e.data, buffer) s = shapely.wkb.loads(bytes(e.data)) assert isinstance(s, Point) assert p.equals(p)
def test_from_shape(): # Standard case: POINT(1 2) expected = WKBElement(str('0101000000000000000000f03f0000000000000040')) p = Point(1, 2) e = from_shape(p) assert isinstance(e, WKBElement) assert isinstance(e.data, buffer) assert e == expected s = shapely.wkb.loads(bytes(e.data)) assert isinstance(s, Point) assert s.equals(p) # Standard case with SRID: SRID=2145;POINT(1 2) expected2 = WKBElement(str('0101000000000000000000f03f0000000000000040'), srid=2154) p = Point(1, 2) e2 = from_shape(p, srid=2154) assert isinstance(e2, WKBElement) assert isinstance(e2.data, buffer) assert e2 == expected2 s2 = shapely.wkb.loads(bytes(e2.data)) assert isinstance(s2, Point) assert s2.equals(p) # Extended case: SRID=2145;POINT(1 2) expected3 = WKBElement( str('01010000206a080000000000000000f03f0000000000000040'), extended=True) e3 = from_shape(p, srid=2154, extended=True) assert isinstance(e3, WKBElement) assert isinstance(e3.data, buffer) assert e3 == expected3 s3 = shapely.wkb.loads(bytes(e3.data)) assert isinstance(s, Point) assert s3.equals(p)
def to_dict(self): """ Returns title number and geojson representing the extent. Everything is stored as a multi-polygon, but we convert to a polygon if the multi-polygon contains only 1 polygon """ shape = mapping(shapely.wkb.loads(bytes(self.extent.data))) extent = {} extent['crs'] = {'type': 'name', 'properties':{'name': 'urn:ogc:def:crs:EPSG:%s' % self.extent.srid}} extent['type'] = 'Feature' extent['properties'] = {} if len(shape['coordinates']) == 1: extent['geometry'] = {'coordinates': shape['coordinates'][0], 'type': 'Polygon'} else: extent['geometry'] = {'coordinates': shape['coordinates'], 'type': 'MultiPolygon'} return {'title_number': self.title_number, 'extent': extent}
def geojson_from_wkbelement(wkb_element): geometry = wkb.loads(bytes(wkb_element.data)) return geojson.dumps(geometry)