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 parseQueryResponse(self, body): try: parsed = loads(body, parse_float=Decimal) base = parsed['@context']['@base'] results = [] for feature in parsed['features']: geometries = feature['geometry']['geometries'] properties = feature['properties'] hgType = properties.get('type') if not hgType: continue pits = [] for pit in properties['pits']: pit['@base'] = base pit['@id'] = base + pit['@id'] geometryIndex = pit['geometryIndex'] pit['geometry'] = Geometry.fromGeoDict(geometries[geometryIndex]) if geometryIndex > -1 else None pits.append(pit) results.append((hgType, pits)) return results except: # print_exc() print 'response body', body import sys; sys.stdout.flush() raise
def testMultiLineStringFromGeoDict(self): json = """{"type":"MultiLineString","coordinates":[[[5.976364581846588,51.52243586973127],[5.977570822531698,51.521009542433255],[5.977641926636947,51.520937272278]],[[5.977641926636947,51.520937272278],[5.9779252893052455,51.52056729706881],[5.978463420127178,51.519845466966835]],[[5.978810297575312,51.51930414638479],[5.978780974683683,51.519300636494314],[5.978753517554276,51.51929103170512],[5.978725963940384,51.519272905985616],[5.978708102058019,51.51925108169847],[5.9786942063007675,51.51923287779468],[5.978688040122361,51.51920855828437],[5.9786858271487935,51.51918908170666],[5.97869714389736,51.519158579206554]],[[5.978463420127178,51.519845466966835],[5.978689959483037,51.51953869936622],[5.97876059153952,51.5194304755717],[5.978810297575312,51.51930414638479]],[[5.978942199072787,51.51912122045602],[5.97897402190174,51.519135261536135],[5.979001702402638,51.51916313044837],[5.979015786946229,51.51919471773031],[5.979020071895229,51.519223927694654],[5.979014461839677,51.51924343492792]],[[5.978942199072787,51.51912122045602],[5.979226543949531,51.518700018617565],[5.979439134138488,51.51842927555684],[5.979760764946663,51.517681570604],[5.979788757821533,51.517618506703975]]]}""" parsed = loads(json, parse_float=Decimal) g = Geometry.fromGeoDict(parsed) r = "MultiLineString([[Decimal('5.976364581846588'), Decimal('51.52243586973127')], [Decimal('5.977570822531698'), Decimal('51.521009542433255')], [Decimal('5.977641926636947'), Decimal('51.520937272278')]], [[Decimal('5.977641926636947'), Decimal('51.520937272278')], [Decimal('5.9779252893052455'), Decimal('51.52056729706881')], [Decimal('5.978463420127178'), Decimal('51.519845466966835')]], [[Decimal('5.978810297575312'), Decimal('51.51930414638479')], [Decimal('5.978780974683683'), Decimal('51.519300636494314')], [Decimal('5.978753517554276'), Decimal('51.51929103170512')], [Decimal('5.978725963940384'), Decimal('51.519272905985616')], [Decimal('5.978708102058019'), Decimal('51.51925108169847')], [Decimal('5.9786942063007675'), Decimal('51.51923287779468')], [Decimal('5.978688040122361'), Decimal('51.51920855828437')], [Decimal('5.9786858271487935'), Decimal('51.51918908170666')], [Decimal('5.97869714389736'), Decimal('51.519158579206554')]], [[Decimal('5.978463420127178'), Decimal('51.519845466966835')], [Decimal('5.978689959483037'), Decimal('51.51953869936622')], [Decimal('5.97876059153952'), Decimal('51.5194304755717')], [Decimal('5.978810297575312'), Decimal('51.51930414638479')]], [[Decimal('5.978942199072787'), Decimal('51.51912122045602')], [Decimal('5.97897402190174'), Decimal('51.519135261536135')], [Decimal('5.979001702402638'), Decimal('51.51916313044837')], [Decimal('5.979015786946229'), Decimal('51.51919471773031')], [Decimal('5.979020071895229'), Decimal('51.519223927694654')], [Decimal('5.979014461839677'), Decimal('51.51924343492792')]], [[Decimal('5.978942199072787'), Decimal('51.51912122045602')], [Decimal('5.979226543949531'), Decimal('51.518700018617565')], [Decimal('5.979439134138488'), Decimal('51.51842927555684')], [Decimal('5.979760764946663'), Decimal('51.517681570604')], [Decimal('5.979788757821533'), Decimal('51.517618506703975')]])" self.assertEquals(r, repr(g)) self.assertEquals(eval(r), g)
def testMultiPolygonFromGeoDict(self): json = """{"type":"MultiPolygon","coordinates":[[[[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]]]]}""" parsed = loads(json, parse_float=Decimal) g = Geometry.fromGeoDict(parsed) 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 testParsePointWkt(self): g = Geometry.parseWkt("POINT(30 10)") self.assertEquals(Point(30, 10), g)
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 testAnnotationFromPit(self): pitToAnnotation = PitToAnnotation(searchApiBaseUrl="http://example.org/search") pit = {'@id': 'nwb/venray-leunseweg', 'data': {'woonplaatsnaam': 'Venray'}, 'type': 'hg:Street', 'name': "Leunseweg", 'geometry': Geometry.fromGeoDict({'type':'MultiLineString', 'coordinates': [[[5.976364581846588,51.52243586973127],[5.977570822531698,51.521009542433255],[5.977641926636947,51.520937272278]],[[5.977641926636947,51.520937272278],[5.9779252893052455,51.52056729706881],[5.978463420127178,51.519845466966835]],[[5.978810297575312,51.51930414638479],[5.978780974683683,51.519300636494314],[5.978753517554276,51.51929103170512],[5.978725963940384,51.519272905985616],[5.978708102058019,51.51925108169847],[5.9786942063007675,51.51923287779468],[5.978688040122361,51.51920855828437],[5.9786858271487935,51.51918908170666],[5.97869714389736,51.519158579206554]],[[5.978463420127178,51.519845466966835],[5.978689959483037,51.51953869936622],[5.97876059153952,51.5194304755717],[5.978810297575312,51.51930414638479]],[[5.978942199072787,51.51912122045602],[5.97897402190174,51.519135261536135],[5.979001702402638,51.51916313044837],[5.979015786946229,51.51919471773031],[5.979020071895229,51.519223927694654],[5.979014461839677,51.51924343492792]],[[5.978942199072787,51.51912122045602],[5.979226543949531,51.518700018617565],[5.979439134138488,51.51842927555684],[5.979760764946663,51.517681570604],[5.979788757821533,51.517618506703975]]]})} annotation = pitToAnnotation.toAnnotation(pit=pit, targetUri='the:uri', query="Leunseweg, Leunen, Venray") self.assertEquals('the:uri', xpathFirst(annotation, '/rdf:RDF/oa:Annotation/oa:hasTarget/@rdf:resource')) self.assertEquals('http://data.digitalecollectie.nl/ns/oa#erfGeoEnriching', xpathFirst(annotation, '/rdf:RDF/oa:Annotation/oa:motivatedBy/@rdf:resource')) self.assertEquals("http://data.digitalecollectie.nl/id/digitalecollectie", xpathFirst(annotation, '/rdf:RDF/oa:Annotation/oa:annotatedBy/@rdf:resource')) self.assertEquals('http://example.org/search?q=Leunseweg%2C+Leunen%2C+Venray', xpathFirst(annotation, '/rdf:RDF/oa:Annotation/dcterms:source/@rdf:resource')) annotationBody = xpathFirst(annotation, '/rdf:RDF/oa:Annotation/oa:hasBody/rdf:Description') placeInTime = xpathFirst(annotationBody, 'dcterms:spatial/hg:PlaceInTime') self.assertEquals("nwb/venray-leunseweg", xpathFirst(placeInTime, '@rdf:about')) self.assertEquals("http://schema.histograph.io/#Street", xpathFirst(placeInTime, 'rdf:type/@rdf:resource')) self.assertEquals('Leunseweg', xpathFirst(placeInTime, 'rdfs:label/text()')) geometry = xpathFirst(placeInTime, 'geos:hasGeometry/rdf:Description/geos:asWKT/text()') self.assertTrue(geometry.startswith('MULTILINESTRING((5.97'), geometry) self.assertEquals('Venray', xpathFirst(placeInTime, 'dcterms:isPartOf/hg:Place/rdfs:label/text()'))
def testPointFromGeoDict(self): json = """{"type":"Point","coordinates":[5.976364581846588,51.52243586973127]}""" parsed = loads(json, parse_float=Decimal) g = Geometry.fromGeoDict(parsed) self.assertEquals(Point(Decimal("5.976364581846588"), Decimal("51.52243586973127")), g) self.assertEquals("Point(Decimal('5.976364581846588'), Decimal('51.52243586973127'))", repr(g))