def run(shapePath): 'Load regions from shapefile' # Parse shapePath shapeName = store.extractFileBaseName(shapePath).decode('utf-8') proj4, shapelyGeometries, fieldPacks, fieldDefinitions = geometry_store.load(shapePath) # Initialize srid = getSRID(proj4) featureCount = 0 # Make tags tagTexts = [ shapeName, ] 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 # 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 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)