def check(rcount_val, vcount_val): vreg.reset_mock() rreg.reset_mock() rcount.return_value = rcount_val vcount.return_value = vcount_val Driver.ensure_registered() if rcount_val: self.assertFalse(rreg.called) else: rreg.assert_called_once_with() if vcount_val: self.assertFalse(vreg.called) else: vreg.assert_called_once_with()
def check(rcount_val, vcount_val): vreg.reset_mock() rreg.reset_mock() rcount.return_value = rcount_val vcount.return_value = vcount_val Driver.ensure_registered() if rcount_val: self.assertFalse(rreg.called) else: rreg.assert_called_once_with() if vcount_val: self.assertFalse(vreg.called) else: vreg.assert_called_once_with()
def get_ogr_db_string(): """ Construct the DB string that GDAL will use to inspect the database. GDAL will create its own connection to the database, so we re-use the connection settings from the Django test. """ db = connections.databases["default"] # Map from the django backend into the OGR driver name and database identifier # https://gdal.org/drivers/vector/ # # TODO: Support Oracle (OCI). drivers = { "django.contrib.gis.db.backends.postgis": ( "PostgreSQL", "PG:dbname='%(db_name)s'", " ", ), "django.contrib.gis.db.backends.mysql": ("MySQL", 'MYSQL:"%(db_name)s"', ","), "django.contrib.gis.db.backends.spatialite": ("SQLite", "%(db_name)s", ""), } db_engine = db["ENGINE"] if db_engine not in drivers: return None drv_name, db_str, param_sep = drivers[db_engine] # Ensure that GDAL library has driver support for the database. try: Driver(drv_name) except GDALException: return None # SQLite/SpatiaLite in-memory databases if db["NAME"] == ":memory:": return None # Build the params of the OGR database connection string params = [db_str % {"db_name": db["NAME"]}] def add(key, template): value = db.get(key, None) # Don't add the parameter if it is not in django's settings if value: params.append(template % value) add("HOST", "host='%s'") add("PORT", "port='%s'") add("USER", "user='******'") add("PASSWORD", "password='******'") return param_sep.join(params)
def get_ogr_db_string(): """ Construct the DB string that GDAL will use to inspect the database. GDAL will create its own connection to the database, so we re-use the connection settings from the Django test. """ db = connections.databases['default'] # Map from the django backend into the OGR driver name and database identifier # http://www.gdal.org/ogr/ogr_formats.html # # TODO: Support Oracle (OCI). drivers = { 'django.contrib.gis.db.backends.postgis': ('PostgreSQL', "PG:dbname='%(db_name)s'", ' '), 'django.contrib.gis.db.backends.mysql': ('MySQL', 'MYSQL:"%(db_name)s"', ','), 'django.contrib.gis.db.backends.spatialite': ('SQLite', '%(db_name)s', '') } db_engine = db['ENGINE'] if db_engine not in drivers: return None drv_name, db_str, param_sep = drivers[db_engine] # Ensure that GDAL library has driver support for the database. try: Driver(drv_name) except: return None # SQLite/Spatialite in-memory databases if db['NAME'] == ":memory:": return None # Build the params of the OGR database connection string params = [db_str % {'db_name': db['NAME']}] def add(key, template): value = db.get(key, None) # Don't add the parameter if it is not in django's settings if value: params.append(template % value) add('HOST', "host='%s'") add('PORT', "port='%s'") add('USER', "user='******'") add('PASSWORD', "password='******'") return param_sep.join(params)
def write_geoq_shape(self, tmp_name, job_pk): job_object = Job.objects.get(id=job_pk) # Get the shapefile driver dr = Driver('ESRI Shapefile') # Creating the datasource ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None) if ds is None: raise Exception('Could not create file!') self.add_aois_to_shapefile(ds, job_object) self.add_features_to_shapefile(ds, job_object) # Cleaning up lgdal.OGR_DS_Destroy(ds) lgdal.OGRCleanupAll()
def get_ogr_db_string(): # Construct the DB string that GDAL will use to inspect the database. # GDAL will create its own connection to the database, so we re-use the # connection settings from the Django test. This approach is a bit fragile # and cannot work on any other database other than PostgreSQL at the moment. db = connections.databases['default'] # Map from the django backend into the OGR driver name and database identifier # http://www.gdal.org/ogr/ogr_formats.html # # TODO: Support Oracle (OCI), MySQL, and SpatiaLite. drivers = { 'django.contrib.gis.db.backends.postgis': ('PostgreSQL', 'PG'), } drv_name, db_str = drivers[db['ENGINE']] # Ensure that GDAL library has driver support for the database. try: Driver(drv_name) except: return None # Build the params of the OGR database connection string # TODO: connection strings are database-dependent, thus if # we ever test other backends, this will need to change. params = ["dbname='%s'" % db['NAME']] def add(key, template): value = db.get(key, None) # Don't add the parameter if it is not in django's settings if value: params.append(template % value) add('HOST', "host='%s'") add('PORT', "port='%s'") add('USER', "user='******'") add('PASSWORD', "password='******'") return '%s:%s' % (db_str, ' '.join(params))
def test03_aliases(self): "Testing driver aliases." for alias, full_name in aliases.items(): dr = Driver(alias) self.assertEqual(full_name, str(dr))
def test01_valid_driver(self): "Testing valid OGR Data Source Drivers." for d in valid_drivers: dr = Driver(d) self.assertEqual(d, str(dr))
def test02_invalid_driver(self): "Testing invalid GDAL/OGR Data Source Drivers." for i in invalid_drivers: with self.assertRaises(GDALException): Driver(i)
def write_with_ctypes_bindings(self, tmp_name, queryset, geo_field): """ Scrive un file in un formato geografico da un geoqueryset; questa funzione usa le librerie Python di GeoDjangos. """ # Get the shapefile driver dr = Driver(self.out_format) # Creating the datasource ds = lgdal.OGR_Dr_CreateDataSource(dr._ptr, tmp_name, None) 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 else: ogr_type = OGRGeomType(geo_field._geom).num # Set up the native spatial reference of geometry field using the srid if hasattr(geo_field, 'srid'): native_srs = SpatialReference(geo_field.srid) else: native_srs = SpatialReference(geo_field._srid) if self.proj_transform: output_srs = SpatialReference(self.proj_transform) else: output_srs = native_srs # create the layer # this is crashing python26 on osx and ubuntu layer = lgdal.OGR_DS_CreateLayer(ds, 'lyr', output_srs._ptr, ogr_type, None) # Create the fields attributes = self.get_attributes() for field in attributes: fld = lgdal.OGR_Fld_Create(str(field.name), 4) 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 self.queryset: feat = lgdal.OGR_F_Create(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 for idx, field in enumerate(attributes): value = getattr(item, field.name) try: string_value = str(value) except UnicodeEncodeError, E: # pass for now.... # http://trac.osgeo.org/gdal/ticket/882 string_value = '' lgdal.OGR_F_SetFieldString(feat, idx, string_value) # 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) if self.proj_transform: ct = CoordTransform(native_srs, output_srs) ogr_geom.transform(ct) # create the geometry check_err(lgdal.OGR_F_SetGeometry(feat, ogr_geom._ptr)) 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(lgdal.OGR_L_SetFeature(layer, feat))