def run(shapePath): 'Load regions from shapefile' # Parse shapePath shapeName = store.extractFileBaseName(shapePath) countryAlpha3, administrativeLevel = re.match(r'(.*)_adm(\d+)', shapeName).groups() countryAlpha3 = countryAlpha3.upper() administrativeLevel = int(administrativeLevel) # Load try: countryName = countryPackByAlpha3[countryAlpha3][0].decode('utf-8') except KeyError: return '%s: Unable to match country code' % shapeName proj4, shapelyGeometries, fieldPacks, fieldDefinitions = geometry_store.load(shapePath) # Initialize srid = getSRID(proj4) featureCount = 0 # Make tags tagTexts = [ countryName + (u' Administrative Level %s' % administrativeLevel if administrativeLevel > 0 else ''), ] tags = model.getTags('\n'.join(tagTexts), addMissing=True) # For each geometry, for shapelyGeometry, fieldPack in itertools.izip(shapelyGeometries, fieldPacks): # Gather properties featureProperties = {} for fieldValue, (fieldName, fieldType) in itertools.izip(fieldPack, fieldDefinitions): if fieldType == osgeo.ogr.OFTString and fieldValue: fieldValue = fieldValue.decode('latin-1') featureProperties[fieldName] = fieldValue if administrativeLevel > 0: featureName = featureProperties['NAME_%s' % administrativeLevel] else: featureName = featureProperties['NAME_ENGLI'] featureProperties['Name'] = featureName # Make feature feature = model.Feature() feature.owner_id = personID feature.geometry = geoalchemy.WKBSpatialElement(buffer(shapelyGeometry.wkb), srid) feature.scope = model.scopePublic feature.properties = featureProperties feature.tags = tags Session.add(feature) # Increment featureCount += 1 # Commit Session.commit() # Return return '%s: %s' % (shapeName, featureCount)
def setUp(self): 'Prepare database' # Prepare people people = [] personPacks = [ ('test_person1', model.hashString('test_person1'), u'test_person1', '*****@*****.**'), ('test_person2', model.hashString('test_person2'), u'test_person2', '*****@*****.**'), ] for personPack in personPacks: person = Session.query(model.Person).filter_by(username=personPack[0]).first() if not person: person = model.Person(*personPack) Session.add(person) people.append(person) Session.commit() self.person1Key, self.person2Key = [x.key for x in people] # Prepare tags tags = [] self.tagTexts = [ u'tag with features that are public', u'tag with features that are private to person1', u'tag with features that are private to person2', ] for tagText in self.tagTexts: tag = Session.query(model.Tag).filter_by(text=tagText).first() if not tag: tag = model.Tag(tagText) Session.add(tag) tags.append(tag) Session.commit() tag1Public, tag1Private, tag2Private = tags # Prepare features features = [] featurePacks = [ (people[0].id, geoalchemy.WKTSpatialElement('POINT(6 10)'), model.scopePublic), (people[0].id, geoalchemy.WKTSpatialElement('LINESTRING(3 4,10 50,20 25)'), model.scopePrivate), (people[1].id, geoalchemy.WKTSpatialElement('POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))'), model.scopePrivate), ] for featurePack in featurePacks: feature = model.Feature() feature.owner_id, feature.geometry, feature.scope = featurePack Session.add(feature) features.append(feature) feature1Public, feature1Private, feature2Private = features feature1Public.tags = [tag1Public] feature1Private.tags = [tag1Private] feature2Private.tags = [tag2Private] Session.commit()
def setUp(self): 'Prepare database' # Prepare people people = [] personPacks = [ ('test_person1', model.hashString('test_person1'), u'test_person1', '*****@*****.**'), ('test_person2', model.hashString('test_person2'), u'test_person2', '*****@*****.**'), ] for personPack in personPacks: person = Session.query( model.Person).filter_by(username=personPack[0]).first() if not person: person = model.Person(*personPack) Session.add(person) people.append(person) Session.commit() self.person1Key, self.person2Key = [x.key for x in people] # Prepare features features = [] featurePacks = [ (people[0].id, geoalchemy.WKTSpatialElement('LINESTRING(3 4,10 50,20 25)')), (people[1].id, geoalchemy.WKTSpatialElement( 'POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))')), ] for featurePack in featurePacks: feature = model.Feature() feature.owner_id = featurePack[0] feature.geometry = featurePack[1] Session.add(feature) features.append(feature) Session.commit() self.feature1ID, self.feature2ID = [x.id for x in features]
def setUp(self): 'Prepare database' # Prepare people people = [] personPacks = [ ('test_person1', model.hashString('test_person1'), u'test_person1', '*****@*****.**'), ('test_person2', model.hashString('test_person2'), u'test_person2', '*****@*****.**'), ] for personPack in personPacks: person = Session.query( model.Person).filter_by(username=personPack[0]).first() if not person: person = model.Person(*personPack) Session.add(person) people.append(person) Session.commit() self.person1Key, self.person2Key = [x.key for x in people] # Prepare tags tags = [] self.tagTexts = [ u'tag with features that are public', u'tag with features that are private to person1', u'tag with features that are private to person2', ] for tagText in self.tagTexts: tag = Session.query(model.Tag).filter_by(text=tagText).first() if not tag: tag = model.Tag(tagText) Session.add(tag) tags.append(tag) Session.commit() tag1Public, tag1Private, tag2Private = tags # Prepare features features = [] featurePacks = [ (people[0].id, geoalchemy.WKTSpatialElement( 'MULTIPOINT (-90.2307590000000062 15.7834710000000005, 126.9779692000000040 37.5665350000000018, -0.1963060000000000 5.5557169999999996, -91.5219589999999954 14.8361560000000008)' ), model.scopePublic, { 'description': 'Santa Eulalia; Seoul; Accra; Xela' }), (people[0].id, geoalchemy.WKTSpatialElement( 'LINESTRING (-87.6297981999999962 41.8781135999999989, -84.3879823999999985 33.7489953999999983, -122.4194154999999995 37.7749294999999989)' ), model.scopePrivate, { 'description': 'Chicago; Atlanta; San Francisco' }), (people[1].id, geoalchemy.WKTSpatialElement( 'LINESTRING (-74.0059731 40.7143528, -90.5352778 14.6133333)' ), model.scopePrivate, { 'passenger': u'Hélène' }), ] for featurePack in featurePacks: feature = model.Feature() feature.owner_id, feature.geometry, feature.scope, feature.properties = featurePack Session.add(feature) features.append(feature) feature1Public, feature1Private, feature2Private = features feature1Public.tags = [tag1Public] feature1Private.tags = [tag1Private] feature2Private.tags = [tag2Private] Session.commit() self.feature1ID = feature1Public.id
try: featureGeometryWKT = geoalchemy.utils.to_wkt(featureGeometry) except (KeyError, TypeError), error: abort(400, 'Could not parse geometry=%s' % featureGeometry) # If featureID is specified, load it if featureID is not None: try: featureID = int(featureID) except ValueError: abort( 400, 'Could not parse featureID=%s as an integer' % featureID) feature = featureByID[featureID] # If featureID is not specified, add it else: feature = model.Feature() feature.owner_id = personID Session.add(feature) # Set feature.properties = featureProperties feature.scope = model.scopePublic if public else model.scopePrivate feature.geometry = geoalchemy.WKTSpatialElement( featureGeometryWKT, srid) feature.tags = tags # Append features.append(feature) # Update timestamps for each tag for tag in tags: tag.updateTimestamp() # Commit Session.commit()