def _get_geometry_type(self, geofield): if hasattr(geofield, 'geom_type'): ogr_type = OGRGeomType(geofield.geom_type).num else: ogr_type = OGRGeomType(geofield._geom).num return ogr_type
def test00b_geomtype_25d(self): "Testing OGRGeomType object with 25D types." wkb25bit = OGRGeomType.wkb25bit self.assertTrue(OGRGeomType(wkb25bit + 1) == 'Point25D') self.assertTrue(OGRGeomType('MultiLineString25D') == (5 + wkb25bit)) self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django)
def _get_geometry_type(self, geofield): if hasattr(geofield, 'geom_type'): geometry_type = OGRGeomType(geofield.geom_type).name else: geometry_type = OGRGeomType(geofield._geom).name return geometry_type
def test_geomtype_25d(self): "Testing OGRGeomType object with 25D types." wkb25bit = OGRGeomType.wkb25bit self.assertEqual(OGRGeomType(wkb25bit + 1), "Point25D") self.assertEqual(OGRGeomType("MultiLineString25D"), (5 + wkb25bit)) self.assertEqual("GeometryCollectionField", OGRGeomType("GeometryCollection25D").django)
def _load_shape_data(self, data_fullpath): """ Load shape area as specified area type from the CBS (or compatible format) shapefiles. """ ds = DataSource(data_fullpath) geom_by_code = {} name_by_code = {} polygon_type = OGRGeomType('Polygon') multipolygon_type = OGRGeomType('MultiPolygon') # Collect possible separate geometries representing the area of a single # municipality. for feature in ds[0]: code = feature.get(self.code_field) name = feature.get(self.name_field) assert name or code # At least one of these must be different from None. if not name and code: name = code # No name present, use code as name. name_by_code[code] = name # Transform to WGS84 and merge if needed. transformed = feature.geom.transform('WGS84', clone=True) if code in geom_by_code: geom_by_code[code] = geom_by_code[code].union(transformed) else: geom_by_code[code] = transformed # Remove previously imported data, save our merged and transformed boundaries to the DB. with transaction.atomic(): Area.objects.filter(_type=self.area_type).delete() for code, geometry in geom_by_code.items(): if geometry.geom_type == polygon_type: geos_polygon = geometry.geos geos_geometry = MultiPolygon(geos_polygon) elif geometry.geom_type == multipolygon_type: geos_geometry = geometry.geos else: raise Exception('Expected either polygon or multipolygon.') Area.objects.create( name=name_by_code[code], code=code, _type=self.area_type, geometry=geos_geometry )
def get_geometry_type(self, table_name, geo_col): cursor = self.connection.cursor() try: # Querying the `geometry_columns` table to get additional metadata. cursor.execute( 'SELECT "coord_dimension", "srid", "type" ' 'FROM "geometry_columns" ' 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s', (table_name, geo_col)) row = cursor.fetchone() if not row: raise Exception( 'Could not find a geometry column for "%s"."%s"' % (table_name, geo_col)) # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. field_type = OGRGeomType(row[2]).django # Getting any GeometryField keyword arguments that are not the default. dim = row[0] srid = row[1] field_params = {} if srid != 4326: field_params['srid'] = srid if isinstance(dim, basestring) and 'Z' in dim: field_params['dim'] = 3 finally: cursor.close() return field_type, field_params
def get_geometry_type(self, table_name, description): """ The geometry type OID used by PostGIS does not indicate the particular type of field that a geometry column is (e.g., whether it's a PointField or a PolygonField). Thus, this routine queries the PostGIS metadata tables to determine the geometry type. """ with self.connection.cursor() as cursor: cursor.execute( """ SELECT t.coord_dimension, t.srid, t.type FROM ( SELECT * FROM geometry_columns UNION ALL SELECT * FROM geography_columns ) AS t WHERE t.f_table_name = %s AND t.f_geometry_column = %s """, (table_name, description.name)) row = cursor.fetchone() if not row: raise Exception( 'Could not find a geometry or geography column for "%s"."%s"' % (table_name, description.name)) dim, srid, field_type = row # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. field_type = OGRGeomType(field_type).django # Getting any GeometryField keyword arguments that are not the default. field_params = {} if self.postgis_oid_lookup.get( description.type_code) == 'geography': field_params['geography'] = True if srid != 4326: field_params['srid'] = srid if dim != 2: field_params['dim'] = dim return field_type, field_params
def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): value = self.deserialize(value) if isinstance(value, dict): value = GEOSGeometry(json.dumps(value), srid=self.map_srid) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except OGRException as err: logger.error( "Error transforming geometry from srid '%s' to srid " "'%s' (%s)" % (value.srid, self.map_srid, err)) context = self.build_attrs( attrs, name=name, module='geodjango_%s' % name.replace('-', '_'), # JS-safe serialized=self.serialize(value), geom_type=OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), ) return loader.render_to_string(self.template_name, context)
class OLMap(self.widget): template = self.map_template geom_type = db_field.geom_type params = {'default_lon' : self.default_lon, 'default_lat' : self.default_lat, 'default_zoom' : self.default_zoom, 'display_wkt' : self.debug or self.display_wkt, 'geom_type' : OGRGeomType(db_field.geom_type), 'field_name' : db_field.name, 'is_collection' : is_collection, 'scrollable' : self.scrollable, 'layerswitcher' : self.layerswitcher, 'collection_type' : collection_type, 'is_linestring' : db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'), 'is_polygon' : db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'), 'is_point' : db_field.geom_type in ('POINT', 'MULTIPOINT'), 'num_zoom' : self.num_zoom, 'max_zoom' : self.max_zoom, 'min_zoom' : self.min_zoom, 'units' : self.units, #likely shoud get from object 'max_resolution' : self.max_resolution, 'max_extent' : self.max_extent, 'modifiable' : self.modifiable, 'mouse_position' : self.mouse_position, 'scale_text' : self.scale_text, 'map_width' : self.map_width, 'map_height' : self.map_height, 'point_zoom' : self.point_zoom, 'srid' : self.map_srid, 'display_srid' : self.display_srid, 'wms_url' : self.wms_url, 'wms_layer' : self.wms_layer, 'wms_name' : self.wms_name, 'debug' : self.debug, }
def get_geometry_type(self, table_name, description): with self.connection.cursor() as cursor: # Querying the `geometry_columns` table to get additional metadata. cursor.execute( "SELECT coord_dimension, srid, geometry_type " "FROM geometry_columns " "WHERE f_table_name=%s AND f_geometry_column=%s", (table_name, description.name), ) row = cursor.fetchone() if not row: raise Exception( 'Could not find a geometry column for "%s"."%s"' % (table_name, description.name)) # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. ogr_type = row[2] if isinstance(ogr_type, int) and ogr_type > 1000: # SpatiaLite uses SFSQL 1.2 offsets 1000 (Z), 2000 (M), and # 3000 (ZM) to indicate the presence of higher dimensional # coordinates (M not yet supported by Django). ogr_type = ogr_type % 1000 + OGRGeomType.wkb25bit field_type = OGRGeomType(ogr_type).django # Getting any GeometryField keyword arguments that are not the default. dim = row[0] srid = row[1] field_params = {} if srid != 4326: field_params["srid"] = srid if (isinstance(dim, str) and "Z" in dim) or dim == 3: field_params["dim"] = 3 return field_type, field_params
def get_geometry_type(self, table_name, geo_col): cursor = self.connection.cursor() try: # Querying the `geometry_columns` table to get additional metadata. type_col = 'type' if self.connection.ops.spatial_version < ( 4, 0, 0) else 'geometry_type' cursor.execute( 'SELECT coord_dimension, srid, %s ' 'FROM geometry_columns ' 'WHERE f_table_name=%%s AND f_geometry_column=%%s' % type_col, (table_name, geo_col)) row = cursor.fetchone() if not row: raise Exception( 'Could not find a geometry column for "%s"."%s"' % (table_name, geo_col)) # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. field_type = OGRGeomType(row[2]).django # Getting any GeometryField keyword arguments that are not the default. dim = row[0] srid = row[1] field_params = {} if srid != 4326: field_params['srid'] = srid if isinstance(dim, six.string_types) and 'Z' in dim: field_params['dim'] = 3 finally: cursor.close() return field_type, field_params
def get_map_widget(self, db_field): """ Return a subclass of the OpenLayersWidget (or whatever was specified in the `widget` attribute) using the settings from the attributes set in this class. """ is_collection = db_field.geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION') if is_collection: if db_field.geom_type == 'GEOMETRYCOLLECTION': collection_type = 'Any' else: collection_type = OGRGeomType(db_field.geom_type.replace('MULTI', '')) else: collection_type = 'None' class OLMap(self.widget): template_name = self.map_template geom_type = db_field.geom_type wms_options = '' if self.wms_options: wms_options = ["%s: '%s'" % pair for pair in self.wms_options.items()] wms_options = ', %s' % ', '.join(wms_options) params = { 'default_lon': self.default_lon, 'default_lat': self.default_lat, 'default_zoom': self.default_zoom, 'display_wkt': self.debug or self.display_wkt, 'geom_type': OGRGeomType(db_field.geom_type), 'field_name': db_field.name, 'is_collection': is_collection, 'scrollable': self.scrollable, 'layerswitcher': self.layerswitcher, 'collection_type': collection_type, 'is_generic': db_field.geom_type == 'GEOMETRY', 'is_linestring': db_field.geom_type in ('LINESTRING', 'MULTILINESTRING'), 'is_polygon': db_field.geom_type in ('POLYGON', 'MULTIPOLYGON'), 'is_point': db_field.geom_type in ('POINT', 'MULTIPOINT'), 'num_zoom': self.num_zoom, 'max_zoom': self.max_zoom, 'min_zoom': self.min_zoom, 'units': self.units, # likely should get from object 'max_resolution': self.max_resolution, 'max_extent': self.max_extent, 'modifiable': self.modifiable, 'mouse_position': self.mouse_position, 'scale_text': self.scale_text, 'map_width': self.map_width, 'map_height': self.map_height, 'point_zoom': self.point_zoom, 'srid': self.map_srid, 'display_srid': self.display_srid, 'wms_url': self.wms_url, 'wms_layer': self.wms_layer, 'wms_name': self.wms_name, 'wms_options': wms_options, 'debug': self.debug, } return OLMap
def get_context(self, name, value, attrs): context = super(BaseGeometryWidget, self).get_context(name, value, attrs) # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if value and isinstance(value, str): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid and value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except OGRException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)", value.srid, self.map_srid, err) if attrs is None: attrs = {} build_attrs_kwargs = { 'name': name, 'module': 'geodjango_%s' % name.replace('-', '_'), # JS-safe 'serialized': self.serialize(value), 'geom_type': OGRGeomType(self.attrs['geom_type']), 'STATIC_URL': settings.STATIC_URL, 'LANGUAGE_BIDI': translation.get_language_bidi(), } build_attrs_kwargs.update(attrs) context.update(self.build_attrs(self.attrs, build_attrs_kwargs)) return context
def get_geometry_type(self, table_name, geo_col): cursor = self.connection.cursor() try: # Querying the `geometry_columns` table to get additional metadata. cursor.execute('SELECT coord_dimension, srid, geometry_type ' 'FROM geometry_columns ' 'WHERE f_table_name=%s AND f_geometry_column=%s', (table_name, geo_col)) row = cursor.fetchone() if not row: raise Exception('Could not find a geometry column for "%s"."%s"' % (table_name, geo_col)) # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. ogr_type = row[2] if isinstance(ogr_type, six.integer_types) and ogr_type > 1000: # SpatiaLite versions >= 4 use the new SFSQL 1.2 offsets # 1000 (Z), 2000 (M), and 3000 (ZM) to indicate the presence of # higher dimensional coordinates (M not yet supported by Django). ogr_type = ogr_type % 1000 + OGRGeomType.wkb25bit field_type = OGRGeomType(ogr_type).django # Getting any GeometryField keyword arguments that are not the default. dim = row[0] srid = row[1] field_params = {} if srid != 4326: field_params['srid'] = srid if (isinstance(dim, six.string_types) and 'Z' in dim) or dim == 3: field_params['dim'] = 3 finally: cursor.close() return field_type, field_params
def set_simple_linestrings(self, tolerance=500): """ Simplifies the source linestrings so they don't use so many points. Provide a tolerance score the indicates how sharply the the lines should be redrawn. Returns True if successful. """ # Get the list of SRIDs we need to update srid_list = self.get_srid_list() # Loop through each for srid in srid_list: # Fetch the source polygon source_field_name = 'linestring_%s' % str(srid) source = getattr(self, source_field_name) # Fetch the target polygon where the result will be saved target_field_name = 'simple_%s' % source_field_name # If there's nothing to transform, drop out now. if not source: setattr(self, target_field_name, None) continue if srid != 900913: # Transform the source out of lng/lat before the simplification copy = source.transform(900913, clone=True) else: copy = deepcopy(source) # Simplify the source simple = copy.simplify(tolerance, True) # If the result is a polygon ... if simple.geom_type == 'LineString': # Create a new Multipolygon shell ml = OGRGeometry(OGRGeomType('MultiLineString')) # Transform the new poly back to its SRID simple.transform(srid) # Stuff it in the shell ml.add(simple.wkt) # Grab the WKT target = ml.wkt # If it's not a polygon... else: # It should be ready to go, so transform simple.transform(srid) # And grab the WKT target = simple.wkt # Stuff the WKT into the field setattr(self, target_field_name, target) return True
def add_aois_to_shapefile(self, ds, job_object): aois = job_object.aois.all() if len(aois) == 0: return geo_field = aois[0].polygon # Get the right geometry type number for ogr ogr_type = OGRGeomType(geo_field.geom_type).num # Set up the native spatial reference of the geometry field using the srid native_srs = SpatialReference(geo_field.srid) # create the AOI layer layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', native_srs._ptr, ogr_type, None) # Create the fields that each feature will have fields = AOI._meta.fields attributes = [] for field in fields: if field.name in 'id, active, name, created_at, updated_at, analyst, priority, status, properties': attributes.append(field) for field in attributes: data_type = 4 if field.name == 'id': data_type = 0 fld = lgdal.OGR_Fld_Create(str(field.name), data_type) added = lgdal.OGR_L_CreateField(layer, fld, 0) check_err(added) # Getting the Layer feature definition. feature_def = lgdal.OGR_L_GetLayerDefn(layer) # Loop through queryset creating features for item in aois: feat = lgdal.OGR_F_Create(feature_def) for idx, field in enumerate(attributes): if field.name == 'properties': value = json.dumps(item.properties) else: value = getattr(item, field.name) string_value = str(value)[:80] lgdal.OGR_F_SetFieldString(feat, idx, string_value) # Transforming & setting the geometry geom = item.polygon ogr_geom = OGRGeometry(geom.wkt, native_srs) check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr)) # create the feature in the layer. check_err(lgdal.OGR_L_CreateFeature(layer, feat)) check_err(lgdal.OGR_L_SyncToDisk(layer))
def _load_cbs_data(self, data_fullpath): """ Load "gemeente", "wijk" or "buurt" areas from the CBS provided shapefiles. """ ds = DataSource(data_fullpath) geom_by_code = {} name_by_code = {} polygon_type = OGRGeomType('Polygon') multipolygon_type = OGRGeomType('MultiPolygon') # Collect possible separate geometries representing the area of a signle # municipality. for feature in ds[0]: code = feature.get(self.code_field) name_by_code[code] = feature.get(self.name_field) # Transform to WGS84 and merge if needed. transformed = feature.geom.transform('WGS84', clone=True) if code in geom_by_code: geom_by_code[code].union(transformed) else: geom_by_code[code] = transformed # Remove previously imported data, save our merged and transformed # municipal boundaries to SIA DB. with transaction.atomic(): Area.objects.filter(_type=self.area_type).delete() for code, geometry in geom_by_code.items(): if geometry.geom_type == polygon_type: geos_polygon = geometry.geos geos_geometry = MultiPolygon(geos_polygon) elif geometry.geom_type == multipolygon_type: geos_geometry = geometry.geos else: raise Exception('Expected either polygon or multipolygon.') Area.objects.create(name=name_by_code[code], code=code, _type=self.area_type, geometry=geos_geometry)
def create_shape_format_layer(headers, geom_type, srid, srid_out=None): """Creates a Shapefile layer definition, that will later be filled with data. :note: All attributes fields have type `String`. """ column_map = {} # Create temp file tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode='w+b', dir=app_settings['TEMP_DIR']) # we must close the file for GDAL to be able to open and write to it tmp.close() # create shape format dr = ogr.GetDriverByName('ESRI Shapefile') ds = dr.CreateDataSource(tmp.name) if ds is None: raise Exception('Could not create file!') ogr_type = OGRGeomType(geom_type).num native_srs = osr.SpatialReference() native_srs.ImportFromEPSG(srid) if srid_out: output_srs = osr.SpatialReference() output_srs.ImportFromEPSG(srid_out) else: output_srs = native_srs layer = ds.CreateLayer('lyr', srs=output_srs, geom_type=ogr_type) if layer is None: raise ValueError('Could not create layer (type=%s, srs=%s)' % (geom_type, output_srs)) # Create other fields for fieldname in headers: field_defn = ogr.FieldDefn(fieldname[:10], ogr.OFTString) field_defn.SetWidth(254) if layer.CreateField(field_defn) != 0: raise Exception('Failed to create field') else: # get name created for each field layerDefinition = layer.GetLayerDefn() column_map[fieldname] = layerDefinition.GetFieldDefn( layerDefinition.GetFieldCount() - 1).GetName() return tmp, layer, ds, native_srs, output_srs, column_map
def add_features_subset_to_shapefile(self, ds, features, layer_name): if len(features) == 0: return geo_field = features[0].the_geom # Get the right geometry type number for ogr ogr_type = OGRGeomType(geo_field.geom_type).num # Set up the native spatial reference of the geometry field using the srid native_srs = SpatialReference(geo_field.srid) # create the Feature layer layer = lgdal.OGR_DS_CreateLayer(ds, layer_name, native_srs._ptr, ogr_type, None) # Create the fields that each feature will have fields = Feature._meta.fields attributes = [] for field in fields: if field.name in 'id, analyst, template, created_at, updated_at, job, project': attributes.append(field) for field in attributes: data_type = 4 if field.name == 'id': data_type = 0 fld = lgdal.OGR_Fld_Create(str(field.name), data_type) added = lgdal.OGR_L_CreateField(layer, fld, 0) check_err(added) # Getting the Layer feature definition. feature_def = lgdal.OGR_L_GetLayerDefn(layer) # Loop through queryset creating features for item in features: feat = lgdal.OGR_F_Create(feature_def) for idx, field in enumerate(attributes): value = getattr(item, field.name) # if field.name == 'template': # value = value.name string_value = str(value) lgdal.OGR_F_SetFieldString(feat, idx, string_value) # Transforming & setting the geometry geom = item.the_geom ogr_geom = OGRGeometry(geom.wkt, native_srs) check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr)) # create the feature in the layer. check_err(lgdal.OGR_L_CreateFeature(layer, feat)) check_err(lgdal.OGR_L_SyncToDisk(layer))
def polygon_to_multipolygon(geom): """ Convert polygons to multipolygons so all features are homogenous in the database. """ if geom.__class__.__name__ == 'Polygon': g = OGRGeometry(OGRGeomType('MultiPolygon')) g.add(geom) return g elif geom.__class__.__name__ == 'MultiPolygon': return geom else: raise ValueError('Geom is neither Polygon nor MultiPolygon.')
def merge(self, other): """ Creates a new MultiPolygon from the Polygons of two MultiPolygons. """ if hasattr(other, 'geometry'): other = other.geometry geometry = OGRGeometry(OGRGeomType('MultiPolygon')) for polygon in self.geometry: geometry.add(polygon) for polygon in other: geometry.add(polygon) return Geometry(geometry)
def geometry_to_multipolygon(geometry): """ Converts a Polygon to a MultiPolygon. """ value = geometry.__class__.__name__ if value == 'MultiPolygon': return geometry elif value == 'Polygon': multipolygon = OGRGeometry(OGRGeomType('MultiPolygon')) multipolygon.add(geometry) return multipolygon else: raise ValueError(_('The geometry is a %(value)s but must be a Polygon or a MultiPolygon.') % {'value': value})
def get_geometry_type(self, table_name, geo_col): """ The geometry type OID used by PostGIS does not indicate the particular type of field that a geometry column is (e.g., whether it's a PointField or a PolygonField). Thus, this routine queries the PostGIS metadata tables to determine the geometry type. """ cursor = self.connection.cursor() try: try: # First seeing if this geometry column is in the `geometry_columns` cursor.execute( 'SELECT "coord_dimension", "srid", "type" ' 'FROM "geometry_columns" ' 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s', (table_name, geo_col), ) row = cursor.fetchone() if not row: raise GeoIntrospectionError except GeoIntrospectionError: cursor.execute( 'SELECT "coord_dimension", "srid", "type" ' 'FROM "geography_columns" ' 'WHERE "f_table_name"=%s AND "f_geography_column"=%s', (table_name, geo_col), ) row = cursor.fetchone() if not row: raise Exception( 'Could not find a geometry or geography column for "%s"."%s"' % (table_name, geo_col)) # OGRGeomType does not require GDAL and makes it easy to convert # from OGC geom type name to Django field. field_type = OGRGeomType(row[2]).django # Getting any GeometryField keyword arguments that are not the default. dim = row[0] srid = row[1] field_params = {} if srid != 4326: field_params["srid"] = srid if dim != 2: field_params["dim"] = dim finally: cursor.close() return field_type, field_params
def get_geometry_type(self, table_name, description): with self.connection.cursor() as cursor: # In order to get the specific geometry type of the field, # we introspect on the table definition using `DESCRIBE`. cursor.execute("DESCRIBE %s" % self.connection.ops.quote_name(table_name)) # Increment over description info until we get to the geometry # column. for column, typ, null, key, default, extra in cursor.fetchall(): if column == description.name: # Using OGRGeomType to convert from OGC name to Django field. # MySQL does not support 3D or SRIDs, so the field params # are empty. field_type = OGRGeomType(typ).django field_params = {} break return field_type, field_params
def handle(self, shapefile, **options): if options['name']: name_field = options['name'] else: name_field = 'NAME' layer = DataSource(shapefile)[0] Location.objects.filter(type=layer.name).delete() for feature in layer: print feature if feature.geom.geom_type.name != 'MultiPolygon': geom = OGRGeometry(OGRGeomType('MultiPolygon')) geom.add(feature.geom) else: geom = feature.geom Location.objects.create(name=feature[name_field], type=layer.name, area=geom.area, geometry=geom.wkt)
def __call__(self, *args, **kwargs): """ """ fields = self.queryset.model._meta.fields geo_fields = [f for f in fields if isinstance(f, GeometryField)] geo_fields_names = ', '.join([f.name for f in geo_fields]) attributes = [f for f in fields if not isinstance(f, GeometryField)] if len(geo_fields) > 1: if not self.geo_field: raise ValueError("More than one geodjango geometry field found, please specify which to use by name using the 'geo_field' keyword. Available fields are: '%s'" % geo_fields_names) else: geo_field_by_name = [fld for fld in geo_fields if fld.name == self.geo_field] if not geo_field_by_name: raise ValueError("Geodjango geometry field not found with the name '%s', fields available are: '%s'" % (self.geo_field,geo_fields_names)) else: geo_field = geo_field_by_name[0] elif geo_fields: geo_field = geo_fields[0] else: raise ValueError('No geodjango geometry fields found in this model queryset') # Get the shapefile driver ###dr = Driver('ESRI Shapefile') dr = ogr.GetDriverByName('ESRI Shapefile') # create a temporary file to write the shapefile to # since we are ultimately going to zip it up tmp = tempfile.NamedTemporaryFile(suffix='.shp', mode='w+b') # we must close the file for GDAL to be able to open and write to it tmp.close() # Creating the datasource ###ds = ogr.OGR_Dr_CreateDataSource(dr._ptr, tmp.name, None) ds = dr.CreateDataSource(tmp.name) if ds is None: raise Exception('Could not create file!') # Get the right geometry type number for ogr if hasattr(geo_field,'geom_type'): ###ogr_type = OGRGeomType(geo_field.geom_type).num ogr_type = OGRGeomType(geo_field.geom_type).num else: ###ogr_type = OGRGeomType(geo_field._geom).num ogr_type = OGRGeomType(geo_field._geom).num # Set up the native spatial reference of the geometry field using the srid native_srs = osr.SpatialReference() if hasattr(geo_field,'srid'): ###native_srs = SpatialReference(geo_field.srid) native_srs.ImportFromEPSG(geo_field.srid) else: ###native_srs = SpatialReference(geo_field._srid) native_srs.ImportFromEPSG(geo_field._srid) ###if self.proj_transform: ### output_srs = SpatialReference(self.proj_transform) ### ct = CoordTransform(native_srs, output_srs) ###else: ### output_srs = native_srs output_srs = native_srs # create the layer # print 'about to try to create data layer' # print 'ds: %s, path: %s' % (ds, tmp.name) ###layer = ogr.OGR_DS_CreateLayer(ds, tmp.name, output_srs._ptr, ogr_type, None) layer = ds.CreateLayer('lyr',srs=output_srs,geom_type=ogr_type) # Create the fields # Todo: control field order as param for field in attributes: ###fld = ogr.OGR_Fld_Create(str(field.name), 4) ###added = ogr.OGR_L_CreateField(layer, fld, 0) ###check_err(added) if field.__class__.__name__ == 'FloatField': field_defn = ogr.FieldDefn(str(field.name),ogr.OFTReal) elif field.__class__.__name__ == 'IntegerField': field_defn = ogr.FieldDefn(str(field.name),ogr.OFTInteger) else: field_defn = ogr.FieldDefn(str(field.name),ogr.OFTString) field_defn.SetWidth(255) if layer.CreateField(field_defn) != 0: raise Exception('Faild to create field') # Getting the Layer feature definition. ###feature_def = ogr.OGR_L_GetLayerDefn(layer) feature_def = layer.GetLayerDefn() # Loop through queryset creating features for item in self.queryset: ###feat = ogr.OGR_F_Create(feature_def) feat = ogr.Feature(feature_def) # For now, set all fields as strings # TODO: catch model types and convert to ogr fields # http://www.gdal.org/ogr/classOGRFeature.html # OGR_F_SetFieldDouble #OFTReal => FloatField DecimalField # OGR_F_SetFieldInteger #OFTInteger => IntegerField #OGR_F_SetFieldStrin #OFTString => CharField # OGR_F_SetFieldDateTime() #OFTDateTime => DateTimeField #OFTDate => TimeField #OFTDate => DateField idx = 0 for field in attributes: value = getattr(item,field.name) if field.__class__.__name__ == 'FloatField': value = float(value) elif field.__class__.__name__ == 'IntegerField': value = int(value) else: try: value = str(value) except UnicodeEncodeError, E: # http://trac.osgeo.org/gdal/ticket/882 value = '' ###ogr.OGR_F_SetFieldString(feat, idx, string_value) #changing the following SetField command from accessing field by name to index #this change solves an issue that arose sometime after gdal 1.6.3 #in which the field names became truncated to 10 chars in CreateField #feat.SetField(str(field.name),string_value) feat.SetField(idx, value) idx += 1 # Transforming & setting the geometry geom = getattr(item,geo_field.name) # if requested we transform the input geometry # to match the shapefiles projection 'to-be' if geom: ###ogr_geom = OGRGeometry(geom.wkt,output_srs) ogr_geom = ogr.CreateGeometryFromWkt(geom.wkt) ###if self.proj_transform: ### ogr_geom.transform(ct) # create the geometry ###check_err(ogr.OGR_F_SetGeometry(feat, ogr_geom._ptr)) check_err(feat.SetGeometry(ogr_geom)) else: # Case where geometry object is not found because of null value for field # effectively looses whole record in shapefile if geometry does not exist pass # creat the feature in the layer. ###check_err(ogr.OGR_L_SetFeature(layer, feat)) check_err(layer.CreateFeature(feat))
class LayerMapping(object): "A class that maps OGR Layers to GeoDjango Models." # Acceptable 'base' types for a multi-geometry type. MULTI_TYPES = { 1: OGRGeomType('MultiPoint'), 2: OGRGeomType('MultiLineString'), 3: OGRGeomType('MultiPolygon'), OGRGeomType('Point25D').num: OGRGeomType('MultiPoint25D'), OGRGeomType('LineString25D').num: OGRGeomType('MultiLineString25D'), OGRGeomType('Polygon25D').num: OGRGeomType('MultiPolygon25D'), } # Acceptable Django field types and corresponding acceptable OGR # counterparts. FIELD_TYPES = { models.AutoField: OFTInteger, models.IntegerField: (OFTInteger, OFTReal, OFTString), models.FloatField: (OFTInteger, OFTReal), models.DateField: OFTDate, models.DateTimeField: OFTDateTime, models.EmailField: OFTString, models.TimeField: OFTTime, models.DecimalField: (OFTInteger, OFTReal), models.CharField: OFTString, models.SlugField: OFTString, models.TextField: OFTString, models.URLField: OFTString, models.BigIntegerField: (OFTInteger, OFTReal, OFTString), models.SmallIntegerField: (OFTInteger, OFTReal, OFTString), models.PositiveSmallIntegerField: (OFTInteger, OFTReal, OFTString), } # The acceptable transaction modes. TRANSACTION_MODES = { 'autocommit': transaction.autocommit, 'commit_on_success': transaction.commit_on_success, } def __init__(self, model, data, mapping, layer=0, source_srs=None, encoding='utf-8', transaction_mode='commit_on_success', transform=True, unique=None, using=None): """ A LayerMapping object is initialized using the given Model (not an instance), a DataSource (or string path to an OGR-supported data file), and a mapping dictionary. See the module level docstring for more details and keyword argument usage. """ # Getting the DataSource and the associated Layer. if isinstance(data, six.string_types): self.ds = DataSource(data, encoding=encoding) else: self.ds = data self.layer = self.ds[layer] self.using = using if using is not None else router.db_for_write(model) self.spatial_backend = connections[self.using].ops # Setting the mapping & model attributes. self.mapping = mapping self.model = model # Checking the layer -- intitialization of the object will fail if # things don't check out before hand. self.check_layer() # Getting the geometry column associated with the model (an # exception will be raised if there is no geometry column). if self.spatial_backend.mysql: transform = False else: self.geo_field = self.geometry_field() # Checking the source spatial reference system, and getting # the coordinate transformation object (unless the `transform` # keyword is set to False) if transform: self.source_srs = self.check_srs(source_srs) self.transform = self.coord_transform() else: self.transform = transform # Setting the encoding for OFTString fields, if specified. if encoding: # Making sure the encoding exists, if not a LookupError # exception will be thrown. from codecs import lookup lookup(encoding) self.encoding = encoding else: self.encoding = None if unique: self.check_unique(unique) transaction_mode = 'autocommit' # Has to be set to autocommit. self.unique = unique else: self.unique = None # Setting the transaction decorator with the function in the # transaction modes dictionary. if transaction_mode in self.TRANSACTION_MODES: self.transaction_decorator = self.TRANSACTION_MODES[ transaction_mode] self.transaction_mode = transaction_mode else: raise LayerMapError('Unrecognized transaction mode: %s' % transaction_mode) #### Checking routines used during initialization #### def check_fid_range(self, fid_range): "This checks the `fid_range` keyword." if fid_range: if isinstance(fid_range, (tuple, list)): return slice(*fid_range) elif isinstance(fid_range, slice): return fid_range else: raise TypeError else: return None def check_layer(self): """ This checks the Layer metadata, and ensures that it is compatible with the mapping information and model. Unlike previous revisions, there is no need to increment through each feature in the Layer. """ # The geometry field of the model is set here. # TODO: Support more than one geometry field / model. However, this # depends on the GDAL Driver in use. self.geom_field = False self.fields = {} # Getting lists of the field names and the field types available in # the OGR Layer. ogr_fields = self.layer.fields ogr_field_types = self.layer.field_types # Function for determining if the OGR mapping field is in the Layer. def check_ogr_fld(ogr_map_fld): try: idx = ogr_fields.index(ogr_map_fld) except ValueError: raise LayerMapError( 'Given mapping OGR field "%s" not found in OGR Layer.' % ogr_map_fld) return idx # No need to increment through each feature in the model, simply check # the Layer metadata against what was given in the mapping dictionary. for field_name, ogr_name in self.mapping.items(): # Ensuring that a corresponding field exists in the model # for the given field name in the mapping. try: model_field = self.model._meta.get_field(field_name) except models.fields.FieldDoesNotExist: raise LayerMapError( 'Given mapping field "%s" not in given Model fields.' % field_name) # Getting the string name for the Django field class (e.g., 'PointField'). fld_name = model_field.__class__.__name__ if isinstance(model_field, GeometryField): if self.geom_field: raise LayerMapError( 'LayerMapping does not support more than one GeometryField per model.' ) # Getting the coordinate dimension of the geometry field. coord_dim = model_field.dim try: if coord_dim == 3: gtype = OGRGeomType(ogr_name + '25D') else: gtype = OGRGeomType(ogr_name) except OGRException: raise LayerMapError( 'Invalid mapping for GeometryField "%s".' % field_name) # Making sure that the OGR Layer's Geometry is compatible. ltype = self.layer.geom_type if not (ltype.name.startswith(gtype.name) or self.make_multi(ltype, model_field)): raise LayerMapError( 'Invalid mapping geometry; model has %s%s, ' 'layer geometry type is %s.' % (fld_name, (coord_dim == 3 and '(dim=3)') or '', ltype)) # Setting the `geom_field` attribute w/the name of the model field # that is a Geometry. Also setting the coordinate dimension # attribute. self.geom_field = field_name self.coord_dim = coord_dim fields_val = model_field elif isinstance(model_field, models.ForeignKey): if isinstance(ogr_name, dict): # Is every given related model mapping field in the Layer? rel_model = model_field.rel.to for rel_name, ogr_field in ogr_name.items(): idx = check_ogr_fld(ogr_field) try: rel_field = rel_model._meta.get_field(rel_name) except models.fields.FieldDoesNotExist: raise LayerMapError( 'ForeignKey mapping field "%s" not in %s fields.' % (rel_name, rel_model.__class__.__name__)) fields_val = rel_model else: raise TypeError( 'ForeignKey mapping must be of dictionary type.') else: # Is the model field type supported by LayerMapping? if not model_field.__class__ in self.FIELD_TYPES: raise LayerMapError( 'Django field type "%s" has no OGR mapping (yet).' % fld_name) # Is the OGR field in the Layer? idx = check_ogr_fld(ogr_name) ogr_field = ogr_field_types[idx] # Can the OGR field type be mapped to the Django field type? if not issubclass(ogr_field, self.FIELD_TYPES[model_field.__class__]): raise LayerMapError( 'OGR field "%s" (of type %s) cannot be mapped to Django %s.' % (ogr_field, ogr_field.__name__, fld_name)) fields_val = model_field self.fields[field_name] = fields_val def check_srs(self, source_srs): "Checks the compatibility of the given spatial reference object." if isinstance(source_srs, SpatialReference): sr = source_srs elif isinstance(source_srs, self.spatial_backend.spatial_ref_sys()): sr = source_srs.srs elif isinstance(source_srs, (int, six.string_types)): sr = SpatialReference(source_srs) else: # Otherwise just pulling the SpatialReference from the layer sr = self.layer.srs if not sr: raise LayerMapError('No source reference system defined.') else: return sr def check_unique(self, unique): "Checks the `unique` keyword parameter -- may be a sequence or string." if isinstance(unique, (list, tuple)): # List of fields to determine uniqueness with for attr in unique: if not attr in self.mapping: raise ValueError elif isinstance(unique, six.string_types): # Only a single field passed in. if unique not in self.mapping: raise ValueError else: raise TypeError( 'Unique keyword argument must be set with a tuple, list, or string.' ) #### Keyword argument retrieval routines #### def feature_kwargs(self, feat): """ Given an OGR Feature, this will return a dictionary of keyword arguments for constructing the mapped model. """ # The keyword arguments for model construction. kwargs = {} # Incrementing through each model field and OGR field in the # dictionary mapping. for field_name, ogr_name in self.mapping.items(): model_field = self.fields[field_name] if isinstance(model_field, GeometryField): # Verify OGR geometry. try: val = self.verify_geom(feat.geom, model_field) except OGRException: raise LayerMapError( 'Could not retrieve geometry from feature.') elif isinstance(model_field, models.base.ModelBase): # The related _model_, not a field was passed in -- indicating # another mapping for the related Model. val = self.verify_fk(feat, model_field, ogr_name) else: # Otherwise, verify OGR Field type. val = self.verify_ogr_field(feat[ogr_name], model_field) # Setting the keyword arguments for the field name with the # value obtained above. kwargs[field_name] = val return kwargs def unique_kwargs(self, kwargs): """ Given the feature keyword arguments (from `feature_kwargs`) this routine will construct and return the uniqueness keyword arguments -- a subset of the feature kwargs. """ if isinstance(self.unique, six.string_types): return {self.unique: kwargs[self.unique]} else: return dict((fld, kwargs[fld]) for fld in self.unique) #### Verification routines used in constructing model keyword arguments. #### def verify_ogr_field(self, ogr_field, model_field): """ Verifies if the OGR Field contents are acceptable to the Django model field. If they are, the verified value is returned, otherwise the proper exception is raised. """ if (isinstance(ogr_field, OFTString) and isinstance(model_field, (models.CharField, models.TextField))): if self.encoding: # The encoding for OGR data sources may be specified here # (e.g., 'cp437' for Census Bureau boundary files). val = force_text(ogr_field.value, self.encoding) else: val = ogr_field.value if model_field.max_length and len( val) > model_field.max_length: raise InvalidString( '%s model field maximum string length is %s, given %s characters.' % (model_field.name, model_field.max_length, len(val))) elif isinstance(ogr_field, OFTReal) and isinstance( model_field, models.DecimalField): try: # Creating an instance of the Decimal value to use. d = Decimal(str(ogr_field.value)) except: raise InvalidDecimal('Could not construct decimal from: %s' % ogr_field.value) # Getting the decimal value as a tuple. dtup = d.as_tuple() digits = dtup[1] d_idx = dtup[2] # index where the decimal is # Maximum amount of precision, or digits to the left of the decimal. max_prec = model_field.max_digits - model_field.decimal_places # Getting the digits to the left of the decimal place for the # given decimal. if d_idx < 0: n_prec = len(digits[:d_idx]) else: n_prec = len(digits) + d_idx # If we have more than the maximum digits allowed, then throw an # InvalidDecimal exception. if n_prec > max_prec: raise InvalidDecimal( 'A DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.' % (model_field.max_digits, model_field.decimal_places, max_prec)) val = d elif isinstance(ogr_field, (OFTReal, OFTString)) and isinstance( model_field, models.IntegerField): # Attempt to convert any OFTReal and OFTString value to an OFTInteger. try: val = int(ogr_field.value) except: raise InvalidInteger('Could not construct integer from: %s' % ogr_field.value) else: val = ogr_field.value return val def verify_fk(self, feat, rel_model, rel_mapping): """ Given an OGR Feature, the related model and its dictionary mapping, this routine will retrieve the related model for the ForeignKey mapping. """ # TODO: It is expensive to retrieve a model for every record -- # explore if an efficient mechanism exists for caching related # ForeignKey models. # Constructing and verifying the related model keyword arguments. fk_kwargs = {} for field_name, ogr_name in rel_mapping.items(): fk_kwargs[field_name] = self.verify_ogr_field( feat[ogr_name], rel_model._meta.get_field(field_name)) # Attempting to retrieve and return the related model. try: return rel_model.objects.using(self.using).get(**fk_kwargs) except ObjectDoesNotExist: raise MissingForeignKey( 'No ForeignKey %s model found with keyword arguments: %s' % (rel_model.__name__, fk_kwargs)) def verify_geom(self, geom, model_field): """ Verifies the geometry -- will construct and return a GeometryCollection if necessary (for example if the model field is MultiPolygonField while the mapped shapefile only contains Polygons). """ # Downgrade a 3D geom to a 2D one, if necessary. if self.coord_dim != geom.coord_dim: geom.coord_dim = self.coord_dim if self.make_multi(geom.geom_type, model_field): # Constructing a multi-geometry type to contain the single geometry multi_type = self.MULTI_TYPES[geom.geom_type.num] g = OGRGeometry(multi_type) g.add(geom) else: g = geom # Transforming the geometry with our Coordinate Transformation object, # but only if the class variable `transform` is set w/a CoordTransform # object. if self.transform: g.transform(self.transform) # Returning the WKT of the geometry. return g.wkt #### Other model methods #### def coord_transform(self): "Returns the coordinate transformation object." SpatialRefSys = self.spatial_backend.spatial_ref_sys() try: # Getting the target spatial reference system target_srs = SpatialRefSys.objects.using( self.using).get(srid=self.geo_field.srid).srs # Creating the CoordTransform object return CoordTransform(self.source_srs, target_srs) except Exception as msg: raise LayerMapError( 'Could not translate between the data source and model geometry: %s' % msg) def geometry_field(self): "Returns the GeometryField instance associated with the geographic column." # Use the `get_field_by_name` on the model's options so that we # get the correct field instance if there's model inheritance. opts = self.model._meta fld, model, direct, m2m = opts.get_field_by_name(self.geom_field) return fld def make_multi(self, geom_type, model_field): """ Given the OGRGeomType for a geometry and its associated GeometryField, determine whether the geometry should be turned into a GeometryCollection. """ return (geom_type.num in self.MULTI_TYPES and model_field.__class__.__name__ == 'Multi%s' % geom_type.django) def save(self, verbose=False, fid_range=False, step=False, progress=False, silent=False, stream=sys.stdout, strict=False): """ Saves the contents from the OGR DataSource Layer into the database according to the mapping dictionary given at initialization. Keyword Parameters: verbose: If set, information will be printed subsequent to each model save executed on the database. fid_range: May be set with a slice or tuple of (begin, end) feature ID's to map from the data source. In other words, this keyword enables the user to selectively import a subset range of features in the geographic data source. step: If set with an integer, transactions will occur at every step interval. For example, if step=1000, a commit would occur after the 1,000th feature, the 2,000th feature etc. progress: When this keyword is set, status information will be printed giving the number of features processed and sucessfully saved. By default, progress information will pe printed every 1000 features processed, however, this default may be overridden by setting this keyword with an integer for the desired interval. stream: Status information will be written to this file handle. Defaults to using `sys.stdout`, but any object with a `write` method is supported. silent: By default, non-fatal error notifications are printed to stdout, but this keyword may be set to disable these notifications. strict: Execution of the model mapping will cease upon the first error encountered. The default behavior is to attempt to continue. """ # Getting the default Feature ID range. default_range = self.check_fid_range(fid_range) # Setting the progress interval, if requested. if progress: if progress is True or not isinstance(progress, int): progress_interval = 1000 else: progress_interval = progress # Defining the 'real' save method, utilizing the transaction # decorator created during initialization. @self.transaction_decorator def _save(feat_range=default_range, num_feat=0, num_saved=0): if feat_range: layer_iter = self.layer[feat_range] else: layer_iter = self.layer for feat in layer_iter: num_feat += 1 # Getting the keyword arguments try: kwargs = self.feature_kwargs(feat) except LayerMapError as msg: # Something borked the validation if strict: raise elif not silent: stream.write('Ignoring Feature ID %s because: %s\n' % (feat.fid, msg)) else: # Constructing the model using the keyword args is_update = False if self.unique: # If we want unique models on a particular field, handle the # geometry appropriately. try: # Getting the keyword arguments and retrieving # the unique model. u_kwargs = self.unique_kwargs(kwargs) m = self.model.objects.using( self.using).get(**u_kwargs) is_update = True # Getting the geometry (in OGR form), creating # one from the kwargs WKT, adding in additional # geometries, and update the attribute with the # just-updated geometry WKT. geom = getattr(m, self.geom_field).ogr new = OGRGeometry(kwargs[self.geom_field]) for g in new: geom.add(g) setattr(m, self.geom_field, geom.wkt) except ObjectDoesNotExist: # No unique model exists yet, create. m = self.model(**kwargs) else: m = self.model(**kwargs) try: # Attempting to save. m.save(using=self.using) num_saved += 1 if verbose: stream.write( '%s: %s\n' % (is_update and 'Updated' or 'Saved', m)) except SystemExit: raise except Exception as msg: if self.transaction_mode == 'autocommit': # Rolling back the transaction so that other model saves # will work. transaction.rollback_unless_managed() if strict: # Bailing out if the `strict` keyword is set. if not silent: stream.write( 'Failed to save the feature (id: %s) into the model with the keyword arguments:\n' % feat.fid) stream.write('%s\n' % kwargs) raise elif not silent: stream.write( 'Failed to save %s:\n %s\nContinuing\n' % (kwargs, msg)) # Printing progress information, if requested. if progress and num_feat % progress_interval == 0: stream.write('Processed %d features, saved %d ...\n' % (num_feat, num_saved)) # Only used for status output purposes -- incremental saving uses the # values returned here. return num_saved, num_feat nfeat = self.layer.num_feat if step and isinstance(step, int) and step < nfeat: # Incremental saving is requested at the given interval (step) if default_range: raise LayerMapError( 'The `step` keyword may not be used in conjunction with the `fid_range` keyword.' ) beg, num_feat, num_saved = (0, 0, 0) indices = range(step, nfeat, step) n_i = len(indices) for i, end in enumerate(indices): # Constructing the slice to use for this step; the last slice is # special (e.g, [100:] instead of [90:100]). if i + 1 == n_i: step_slice = slice(beg, None) else: step_slice = slice(beg, end) try: num_feat, num_saved = _save(step_slice, num_feat, num_saved) beg = end except: stream.write('%s\nFailed to save slice: %s\n' % ('=-' * 20, step_slice)) raise else: # Otherwise, just calling the previously defined _save() function. _save()
def check_layer(self): """ This checks the Layer metadata, and ensures that it is compatible with the mapping information and model. Unlike previous revisions, there is no need to increment through each feature in the Layer. """ # The geometry field of the model is set here. # TODO: Support more than one geometry field / model. However, this # depends on the GDAL Driver in use. self.geom_field = False self.fields = {} # Getting lists of the field names and the field types available in # the OGR Layer. ogr_fields = self.layer.fields ogr_field_types = self.layer.field_types # Function for determining if the OGR mapping field is in the Layer. def check_ogr_fld(ogr_map_fld): try: idx = ogr_fields.index(ogr_map_fld) except ValueError: raise LayerMapError( 'Given mapping OGR field "%s" not found in OGR Layer.' % ogr_map_fld) return idx # No need to increment through each feature in the model, simply check # the Layer metadata against what was given in the mapping dictionary. for field_name, ogr_name in self.mapping.items(): # Ensuring that a corresponding field exists in the model # for the given field name in the mapping. try: model_field = self.model._meta.get_field(field_name) except models.fields.FieldDoesNotExist: raise LayerMapError( 'Given mapping field "%s" not in given Model fields.' % field_name) # Getting the string name for the Django field class (e.g., 'PointField'). fld_name = model_field.__class__.__name__ if isinstance(model_field, GeometryField): if self.geom_field: raise LayerMapError( 'LayerMapping does not support more than one GeometryField per model.' ) # Getting the coordinate dimension of the geometry field. coord_dim = model_field.dim try: if coord_dim == 3: gtype = OGRGeomType(ogr_name + '25D') else: gtype = OGRGeomType(ogr_name) except OGRException: raise LayerMapError( 'Invalid mapping for GeometryField "%s".' % field_name) # Making sure that the OGR Layer's Geometry is compatible. ltype = self.layer.geom_type if not (ltype.name.startswith(gtype.name) or self.make_multi(ltype, model_field)): raise LayerMapError( 'Invalid mapping geometry; model has %s%s, ' 'layer geometry type is %s.' % (fld_name, (coord_dim == 3 and '(dim=3)') or '', ltype)) # Setting the `geom_field` attribute w/the name of the model field # that is a Geometry. Also setting the coordinate dimension # attribute. self.geom_field = field_name self.coord_dim = coord_dim fields_val = model_field elif isinstance(model_field, models.ForeignKey): if isinstance(ogr_name, dict): # Is every given related model mapping field in the Layer? rel_model = model_field.rel.to for rel_name, ogr_field in ogr_name.items(): idx = check_ogr_fld(ogr_field) try: rel_field = rel_model._meta.get_field(rel_name) except models.fields.FieldDoesNotExist: raise LayerMapError( 'ForeignKey mapping field "%s" not in %s fields.' % (rel_name, rel_model.__class__.__name__)) fields_val = rel_model else: raise TypeError( 'ForeignKey mapping must be of dictionary type.') else: # Is the model field type supported by LayerMapping? if not model_field.__class__ in self.FIELD_TYPES: raise LayerMapError( 'Django field type "%s" has no OGR mapping (yet).' % fld_name) # Is the OGR field in the Layer? idx = check_ogr_fld(ogr_name) ogr_field = ogr_field_types[idx] # Can the OGR field type be mapped to the Django field type? if not issubclass(ogr_field, self.FIELD_TYPES[model_field.__class__]): raise LayerMapError( 'OGR field "%s" (of type %s) cannot be mapped to Django %s.' % (ogr_field, ogr_field.__name__, fld_name)) fields_val = model_field self.fields[field_name] = fields_val
def render(self, name, value, attrs=None): # Update the template parameters with any attributes passed in. # If value is None, that means we haven't saved anything yet. if attrs: self.params.update(attrs) # Defaulting the WKT value to a blank string -- this # will be tested in the JavaScript and the appropriate # interface will be constructed. self.params['wkt'] = '' # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, basestring): try: value = GEOSGeometry(value) except (GEOSException, ValueError): value = None if value and value.geom_type.upper( ) != self.geom_type and self.geom_type != 'GEOMETRY': value = None # Constructing the dictionary of the map options. self.params['map_options'] = self.map_options() # Constructing the JavaScript module name using the name of # the GeometryField (passed in via the `attrs` keyword). # Use the 'name' attr for the field name (rather than 'field') self.params['name'] = name # note: we must switch out dashes for underscores since js # functions are created using the module variable js_safe_name = self.params['name'].replace('-', '_') self.params['module'] = 'geodjango_%s' % js_safe_name if value: # Transforming the geometry to the projection used on the # OpenLayers map. srid = self.params['srid'] if value.srid != srid: try: ogr = value.ogr ogr.transform(srid) wkt = ogr.wkt except OGRException: wkt = '' else: wkt = value.wkt # Setting the parameter WKT with that of the transformed # geometry. self.params['wkt'] = wkt # Check if the field is generic so the proper values are overriden if self.params['is_unknown']: self.params['geom_type'] = OGRGeomType(value.geom_type) if value.geom_type.upper() in ('LINESTRING', 'MULTILINESTRING'): self.params['is_linestring'] = True elif value.geom_type.upper() in ('POLYGON', 'MULTIPOLYGON'): self.params['is_polygon'] = True elif value.geom_type.upper() in ('POINT', 'MULTIPOINT'): self.params['is_point'] = True elif value.geom_type.upper() in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON'): self.params['is_collection'] = True self.params['collection_type'] = OGRGeomType( value.geom_type.upper().replace('MULTI', '')) elif value.geom_type.upper() == 'GEOMETRYCOLLECTION': self.params['is_collection'] = True self.params['collection_type'] = 'Any' # Avoid 'Collection', see http://trac.osgeo.org/openlayers/ticket/2240 #self.params['geom_type'] = 'Collection' self.params['geom_type'] = OGRGeomType('POLYGON') else: # No value. if self.params['is_unknown']: # If the geometry is unknown and the value is not set, make it as flexible as possible. # But again, due to http://trac.osgeo.org/openlayers/ticket/2240 # we can't safely use Collection. self.params['geom_type'] = OGRGeomType( 'POLYGON') #'Collection' self.params['is_collection'] = True self.params['collection_type'] = 'Any' # If we don't already have a camelcase geom_type, # make one using str(OGRGeomType). # Works for most things but not 'GeometryCollection'. if str(self.params['geom_type']) == str( self.params['geom_type']).upper(): self.params['geom_type'] = str( OGRGeomType(self.params['geom_type'])) return loader.render_to_string(self.template, self.params, context_instance=geo_context)
def get_map_widget(self, db_field): """ Returns a subclass of the OpenLayersWidget (or whatever was specified in the `widget` attribute) using the settings from the attributes set in this class. OVERRIDING FOR OPENBLOCK: This is the patched version of this method as per http://code.djangoproject.com/attachment/ticket/9806/9806.3.diff and we can maybe delete it if/when http://code.djangoproject.com/ticket/9806 gets fixed. ... Or not: actually we want to disable GEOMETRYCOLLECTIONs entirely, due to bug #95, and I'm not sure if that's appropriate to submit upstream or not. """ # Note that db_field.geom_type is an UPPERCASE name, while # OGRGeomType(foo) yields a CamelCase name. geom_type = db_field.geom_type.upper() is_unknown = geom_type in ('GEOMETRY', ) if not is_unknown: #If it is not generic, get the parameters from the db_field. is_collection = geom_type in ('MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON', 'GEOMETRYCOLLECTION') if is_collection: if geom_type == 'GEOMETRYCOLLECTION': # Workaround for #95: Use MultiPolygon instead of GeometryCollection. geom_type = 'MULTIPOLYGON' collection_type = OGRGeomType(geom_type.replace('MULTI', '')) else: collection_type = 'None' is_linestring = geom_type in ('LINESTRING', 'MULTILINESTRING') is_polygon = geom_type in ('POLYGON', 'MULTIPOLYGON') is_point = geom_type in ('POINT', 'MULTIPOINT') openlayers_geom_type = OGRGeomType(geom_type) else: #If it is generic, set sensible defaults. #We've decided this will be MultiPolygon. is_collection = True collection_type = 'Polygon' is_linestring = False is_polygon = True is_point = False openlayers_geom_type = OGRGeomType(geom_type.upper()) class OLMap(self.widget): template = self.map_template geom_type = db_field.geom_type wms_options = '' if self.wms_options: wms_options = [ "%s: '%s'" % pair for pair in self.wms_options.items() ] wms_options = ', '.join(wms_options) wms_options = ', ' + wms_options params = { 'default_lon': self.default_lon, 'collection_type': collection_type, 'debug': self.debug, 'default_lat': self.default_lat, 'default_zoom': self.default_zoom, 'display_srid': self.display_srid, 'display_wkt': self.debug or self.display_wkt, 'field_name': db_field.name, 'geom_type': openlayers_geom_type, # a camel-case name for use as an OpenLayers constructor. 'is_collection': is_collection, 'is_linestring': is_linestring, 'is_point': is_point, 'is_polygon': is_polygon, 'is_unknown': is_unknown, 'layerswitcher': self.layerswitcher, 'map_height': self.map_height, 'map_width': self.map_width, 'max_extent': self.max_extent, 'max_resolution': self.max_resolution, 'max_zoom': self.max_zoom, 'min_zoom': self.min_zoom, 'modifiable': self.modifiable, 'mouse_position': self.mouse_position, 'num_zoom': self.num_zoom, 'openlayers_url': self.openlayers_url, 'openlayers_img_path': self.openlayers_img_path, 'point_zoom': self.point_zoom, 'scale_text': self.scale_text, 'scrollable': self.scrollable, 'srid': self.map_srid, 'units': self.units, #likely shoud get from object 'wms_layer': self.wms_layer, 'wms_name': self.wms_name, 'wms_options': wms_options, 'wms_url': self.wms_url, } return OLMap