def test_linestring(self): f = feature(self.placemarks[1]) self.failUnless(f.geometry.type == f["geometry"]["type"] == "LineString") coords0 = f.geometry.coordinates[0] self.failUnlessCoordsAlmostEqual(coords0, (-122.36438, 37.82466, 0.0), 5) self.failUnless(f.properties["name"] == f["properties"]["name"] == "linestring") self.failUnless(f.properties["snippet"] == f["properties"]["snippet"] == "LineString test") self.failUnless(f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah")
def test_point(self): f = feature(self.placemarks[0]) self.failUnless(f.geometry.type == f["geometry"]["type"] == "Point") coords = f.geometry.coordinates self.failUnlessCoordsAlmostEqual(coords, (-122.36438, 37.82466, 0.0), 5) self.failUnless(f.properties["name"] == f["properties"]["name"] == "point") self.failUnless(f.properties["snippet"] == f["properties"]["snippet"] == "Point test") self.failUnless(f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah")
def test_track(placemarks): f = feature(placemarks[2]) assert f.geometry.type == f["geometry"]["type"] == "LineString" coords0 = f.geometry.coordinates[0] assert coords0 == pytest.approx((-122.36438, 37.82466, 0.0), 5) assert f.properties["name"] == f["properties"]["name"] == "track" assert f.properties["snippet"] == f["properties"]["snippet"] == "Track test" assert (f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah")
def test_point(placemarks): f = feature(placemarks[0]) assert f.geometry.type == f["geometry"]["type"] == "Point" coords = f.geometry.coordinates assert coords == pytest.approx((-122.36438, 37.82466, 0.0), 5) assert f.properties["name"] == f["properties"]["name"] == "point" assert f.properties["snippet"] == f["properties"]["snippet"] == "Point test" assert (f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah")
def test_polygon(self): f = feature(self.placemarks[2]) self.failUnless(f.geometry.type == f["geometry"]["type"] == "Polygon") coords0 = f.geometry.coordinates[0][0] self.failUnlessCoordsAlmostEqual(coords0, (-122.366278, 37.81884, 30.0), 5) self.failUnless(f.properties["name"] == f["properties"]["name"] == "polygon") self.failUnless(f.properties["snippet"] == f["properties"]["snippet"] == "Polygon test") self.failUnless(f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah") coords1 = f.geometry.coordinates[1][0] self.failUnlessCoordsAlmostEqual(coords1, (-122.366212, 37.818977, 30.0), 5)
def test_polygon(placemarks): f = feature(placemarks[4]) assert f.geometry.type == f["geometry"]["type"] == "Polygon" coords0 = f.geometry.coordinates[0][0] assert coords0 == pytest.approx((-122.366278, 37.81884, 30.0), 5) assert f.properties["name"] == f["properties"]["name"] == "polygon" assert f.properties["snippet"] == f["properties"][ "snippet"] == "Polygon test" assert (f.properties["description"] == f["properties"]["description"] == "Blah, blah, blah") coords1 = f.geometry.coordinates[1][0] assert coords1 == pytest.approx((-122.366212, 37.818977, 30.0), 5)
def test_point(self): f = feature(self.placemarks[0]) self.failUnless(f.geometry.type == f['geometry']['type'] == 'Point') coords = f.geometry.coordinates self.failUnlessCoordsAlmostEqual(coords, (-122.36438, 37.82466, 0.0), 5) self.failUnless( f.properties['name'] == f['properties']['name'] == 'point') self.failUnless( f.properties['snippet'] == f['properties']['snippet'] \ == 'Point test' ) self.failUnless( f.properties['description'] == f['properties']['description'] \ == 'Blah, blah, blah' )
def populateServiceBoundary(): kmlFile = None try: if conf: kmlFile = g_grpmDir + conf['kmlFile'] except Exception as e: dbLogger.error("Failed to load KML file. Exception: " + str(e)) return serv_bounds = {} doc = open(kmlFile, "rb").read() tree = ElementTree.fromstring(str(doc)) kmlns = tree.tag.split('}')[0][1:] pmarks = tree.findall("*/{%s}Placemark" % kmlns) for pmark in pmarks: tag = re.findall("}(\w+)", str(pmark[0]))[0] if tag == "name": kmlNetwork = str(pmark[0].text) f = keytree.feature(pmark) shape = asShape(f.geometry) serv_bounds[str(kmlNetwork)] = shape # if multigeometry mgs = tree.findall("*/{%s}MultiGeometry" % kmlns) for mg in mgs: for pm in mg: tag = re.findall("}(\w+)", str(pm[0]))[0] if tag == "name": kmlNetwork = str(pm[0].text) #dbLogger.info("kmlNetwork:"+str(kmlNetwork)) f = keytree.feature(pm) shape = asShape(f.geometry) if str(kmlNetwork) in serv_bounds.keys(): serv_bounds[str(kmlNetwork)].append(shape) else: serv_bounds[str(kmlNetwork)] = [shape] dbLogger.info("serv_bounds:" + str(serv_bounds)) return serv_bounds
def test_linestring(self): f = feature(self.placemarks[1]) self.failUnless( f.geometry.type == f['geometry']['type'] == 'LineString') coords0 = f.geometry.coordinates[0] self.failUnlessCoordsAlmostEqual(coords0, (-122.36438, 37.82466, 0.0), 5) self.failUnless( f.properties['name'] == f['properties']['name'] == 'linestring') self.failUnless( f.properties['snippet'] == f['properties']['snippet'] \ == 'LineString test' ) self.failUnless( f.properties['description'] == f['properties']['description'] \ == 'Blah, blah, blah' )
def test_polygon(self): f = feature(self.placemarks[2]) self.failUnless(f.geometry.type == f['geometry']['type'] == 'Polygon') coords0 = f.geometry.coordinates[0][0] self.failUnlessCoordsAlmostEqual(coords0, (-122.366278, 37.81884, 30.0), 5) self.failUnless( f.properties['name'] == f['properties']['name'] == 'polygon') self.failUnless( f.properties['snippet'] == f['properties']['snippet'] \ == 'Polygon test' ) self.failUnless( f.properties['description'] == f['properties']['description'] \ == 'Blah, blah, blah' ) coords1 = f.geometry.coordinates[1][0] self.failUnlessCoordsAlmostEqual(coords1, (-122.366212, 37.818977, 30.0), 5)
def __call__(self, kml): portal = getToolByName(self.context, 'portal_url').getPortalObject() ptool = getToolByName(self.context, 'plone_utils') wtool = getToolByName(self.context, 'portal_workflow') places = portal['places'] features = portal['features'] savepoint = transaction.savepoint() try: k = etree.fromstring(kml) kmlns = k.tag.split('}')[0][1:] # metadata doc dtitle = getattr(k.find('*/{%s}name' % kmlns), 'text', 'Unnamed KML Document') mdid = features['metadata'].invokeFactory('PositionalAccuracy', str(uuid.uuid4()), title=dtitle, value=100.0, source=kml) features['metadata'][mdid].setTitle(dtitle) features['metadata'][mdid].setValue(100.0) features['metadata'][mdid].setSource(kml) features['metadata'][mdid].source.filename = dtitle features['metadata'][mdid].source.content_type = 'application/vnd.google-earth.kml+xml' self.context.attach(features['metadata'][mdid]) # Build up a mapping of place and location elements kmlplaces = {} for pm_element in k.xpath('//kml:Placemark', namespaces={'kml': kmlns}): parent = pm_element.getparent() if parent.tag == "{%s}Folder" % kmlns: if parent not in kmlplaces: kmlplaces[parent] = [] kmlplaces[parent].append(pm_element) else: kmlplaces[pm_element] = [pm_element] # Marshal them into Pleiades content objects for i, (place, locations) in enumerate(kmlplaces.items()): # Do place kmlid = place.attrib.get('id') title = getattr(place.find('{%s}name' % kmlns), 'text', 'Unnamed Place') description = getattr(place.find('{%s}Snippet' % kmlns), 'text', "%s: %s" % (title, (kmlid or "%s of %s imported places" % (i+1, len(kmlplaces))))) text = getattr(place.find('{%s}description' % kmlns), 'text', '') pid = places.invokeFactory( 'Place', places.generateId(prefix=''), title=title, description=description, text=text ) places[pid].setTitle(title) places[pid].setDescription(description) places[pid].setText(text) self.context.attach(places[pid]) posAccDoc = features['metadata'][mdid] places[pid].addReference(posAccDoc, 'location_accuracy') # TODO: names for j, location in enumerate(locations): f = keytree.feature(location) if location is not place: name = f.properties['name'] description = getattr( location.find('{%s}Snippet' % kmlns), 'text', 'Imported KML Placemark') text = getattr( pm_element.find('{%s}description' % kmlns), 'text', '') title = name or 'Unnamed Location' else: name = title = 'Position' description = 'Nominal position of the ancient place' text = '' # TODO: parse out temporal information # Process a geo-interface provider into strict GeoJSON # shorthand. where = geojson.GeoJSON.to_instance(dict( type=f.geometry.type, coordinates=f.geometry.coordinates)) data = geojson.loads(geojson.dumps(where)) geometry = '%s:%s' % (str(data['type']), data['coordinates']) lid = places[pid].invokeFactory('Location', ptool.normalizeString(name or "location-%s" % (f.id or j)), title=title, description=description, text=text, geometry=geometry) places[pid][lid].setTitle(title) places[pid][lid].setDescription(description) places[pid][lid].setText(text) #transliteration = name.encode('utf-8') #nid = f.invokeFactory( # 'Name', # ptool.normalizeString(transliteration), # nameTransliterated=transliteration # ) places[pid][lid].addReference(posAccDoc, 'location_accuracy') except: savepoint.rollback() raise transaction.commit()
def test_properties_context(self): f = feature(self.placemarks[0]) props = f.properties self.failUnless('name' in props['@context']) self.failUnless('snippet' in props['@context']) self.failUnless('description' in props['@context'])
def test_multilinestring(placemarks): f = feature(placemarks[5]) assert f.geometry.type == f["geometry"]["type"] == "MultiLineString"
def test_properties_context(placemarks): f = feature(placemarks[0]) props = f.properties assert "name" in props["@context"] assert "snippet" in props["@context"] assert "description" in props["@context"]
def test_properties_context(self): f = feature(self.placemarks[0]) props = f.properties self.assertTrue("name" in props["@context"]) self.assertTrue("snippet" in props["@context"]) self.assertTrue("description" in props["@context"])