def testParseMultiPolygonWkt(self): g = Geometry.parseWkt( "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))" ) r = "MultiPolygon([[[40, 40], [20, 45], [45, 30], [40, 40]]], [[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]], [[30, 20], [20, 15], [20, 25], [30, 20]]])" self.assertEquals(r, repr(g)) self.assertEquals(eval(r), g)
def testMultiPolygonAsWkt(self): g = MultiPolygon( [[[40, 40], [20, 45], [45, 30], [40, 40]]], [[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]], [[30, 20], [20, 15], [20, 25], [30, 20]]], ) self.assertEquals( "MULTIPOLYGON(((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))", g.asWkt(), ) self.assertEquals(g, Geometry.parseWkt(g.asWkt()))
def testPointCoordinates(self): g = Geometry.parseWkt("POINT(30 10)") self.assertEquals([(30, 10)], asList(g.pointCoordinates())) g = Geometry.parseWkt("POINT(30.12 10.07)") self.assertEquals([(30.12, 10.07)], asList(g.pointCoordinates())) g = Geometry.parseWkt("POINT(5.979788757821533 51.517618506703975)") self.assertEquals([(5.979788757821533, 51.517618506703975)], asList(g.pointCoordinates())) g = Point(Decimal("5.976364581846588"), Decimal("51.52243586973127")) self.assertEquals([(Decimal("5.976364581846588"), Decimal("51.52243586973127"))], asList(g.pointCoordinates())) g = MultiLineString([[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]]) self.assertEquals( [(10, 10), (20, 20), (10, 40), (40, 40), (30, 30), (40, 20), (30, 10)], asList(g.pointCoordinates()) ) g = MultiPolygon( [[[40, 40], [20, 45], [45, 30], [40, 40]]], [[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]], [[30, 20], [20, 15], [20, 25], [30, 20]]], ) self.assertEquals( [ (40, 40), (20, 45), (45, 30), (40, 40), (20, 35), (10, 30), (10, 10), (30, 5), (45, 20), (20, 35), (30, 20), (20, 15), (20, 25), (30, 20), ], asList(g.pointCoordinates()), )
def _evaluate(self, fieldname, value): if fieldname.startswith('dcterms:spatial.') and fieldname.endswith('.geos:asWKT'): geometry = Geometry.parseWkt(value) for (geoLong, geoLat) in geometry.pointCoordinates(): yield 'dcterms:spatial.geo:long', geoLong yield 'dcterms:spatial.geo:lat', geoLat return if fieldname == 'dcterms:spatial.uri': if value.startswith('geo:'): geoLat, _, geoLong = value[len('geo:'):].partition(',') yield 'dcterms:spatial.geo:long', geoLong yield 'dcterms:spatial.geo:lat', geoLat return if fieldname in ['dc:date', 'dcterms:created']: for year in parseYears(value): yield 'dc:date.year', str(year) yield fieldname, value
def _geoCoordinatesPresent(self, summary): annotationBody = xpathFirst(summary, 'oa:Annotation/oa:hasBody/*') nodes = [annotationBody] for uri in xpath(annotationBody, 'dcterms:spatial/@rdf:resource'): node = xpathFirst(summary, '*[@rdf:about="%s"]') if not node is None: nodes.append(node) for node in xpath(annotationBody, 'dcterms:spatial/*'): nodes.append(node) for node in nodes: geoLat = xpathFirst(node, 'geo:lat/text()') geoLong = xpathFirst(node, 'geo:long/text()') if geoLat and geoLong: return (geoLat, geoLong) asWkt = xpathFirst(node, 'geos:hasGeometry/*/geos:asWKT/text()') if not asWkt is None: return Geometry.parseWkt(asWkt).pointCoordinates().next() return None
def testMultiLineStringAsWkt(self): g = MultiLineString([[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]]) self.assertEquals("MULTILINESTRING((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))", g.asWkt()) self.assertEquals(g, Geometry.parseWkt(g.asWkt()))
def testParseMultiLineStringWkt(self): g = Geometry.parseWkt("MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))") r = "MultiLineString([[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]])" self.assertEquals(r, repr(g)) self.assertEquals(eval(r), g)
def testParsePolygonWkt(self): g = Geometry.parseWkt("POLYGON ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))") r = "Polygon([[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]])" self.assertEquals(r, repr(g)) self.assertEquals(eval(r), g)
def testPointAsWkt(self): g = Geometry.fromGeoDict({"type": "Point", "coordinates": (5.97978875782, 51.5176185067)}) self.assertEquals("POINT(5.97978875782 51.5176185067)", g.asWkt()) self.assertEquals(g, Geometry.parseWkt(g.asWkt()))
def testParseWktWithScientificNotationCoordinates(self): g = Geometry.parseWkt("POINT(1.6763812E-7 1.6763812E-7)") self.assertEquals(Point(1.6763812e-7, 1.6763812e-7), g)
def testParseWktWithNegations(self): g = Geometry.parseWkt("POINT(-30.3 -10.2)") self.assertEquals(Point(-30.3, -10.2), g)
def testParsePointWkt(self): g = Geometry.parseWkt("POINT(30 10)") self.assertEquals(Point(30, 10), g)