def get_capabilities_response(self, request, params): return render_to_response( 'ga_ows/WFS_GetCapabilities.template.xml', { "title": self.title, "keywords": self.keywords, "fees": self.fees, "access_constraints": self.access_constraints, "endpoint": request.build_absolute_uri().split('?')[0], "output_formats": [ ogr.GetDriver(drv).GetName() for drv in range(ogr.GetDriverCount()) if ogr.GetDriver( drv).TestCapability(ogr.ODrCCreateDataSource) ], "addr_street": self.addr_street, "addr_city": self.addr_city, "addr_admin_area": self.addr_admin_area, "addr_postcode": self.addr_postcode, "addr_country": self.addr_country, "feature_versioning": self.adapter.supports_feature_versioning(), "transactional": True, 'feature_types': self.adapter.get_feature_descriptions(request) })
def vectInfo(conf,inputs,outputs): global bReadOnly global bVerbose global bSummaryOnly global nFetchFID global papszOptions pszWHERE = None pszDataSource = None papszLayers = None poSpatialFilter = None nRepeatCount = 1 bAllLayers = False pszSQLStatement = None pszDialect = None options = {} pszSQLStatement = inputs["q"]["value"] pszDataSource = inputs["dstName"]["value"] papszLayers.append(inputs["dsoName"]["value"]) #/* -------------------------------------------------------------------- */ #/* Open data source. */ #/* -------------------------------------------------------------------- */ poDS = None poDriver = None poDS = ogr.Open( pszDataSource, not bReadOnly ) if poDS is None and not bReadOnly: poDS = ogr.Open( pszDataSource, False ) if poDS is not None and bVerbose: print >> sys.stderr,"Had to open data source read-only." bReadOnly = True #/* -------------------------------------------------------------------- */ #/* Report failure */ #/* -------------------------------------------------------------------- */ if poDS is None: print >>sys.stderr, "FAILURE:\n"\ "Unable to open datasource `%s' with the following drivers." % pszDataSource conf["lenv"]["message"]="FAILURE:\n"\ "Unable to open datasource `%s' with the following drivers." % pszDataSource for iDriver in range(ogr.GetDriverCount()): print >>sys.stderr," -> %s" % ogr.GetDriver(iDriver).GetName() conf["lenv"]["message"]+=" -> %s" % ogr.GetDriver(iDriver).GetName() return 4 poDriver = poDS.GetDriver() #/* -------------------------------------------------------------------- */ #/* Some information messages. */ #/* -------------------------------------------------------------------- */ if bVerbose: print >>sys.stderr,"INFO: Open of `%s'\n" " using driver `%s' successful." % (pszDataSource, poDriver.GetName())
def get_ogr_driver(filepath): """ Get the OGR driver from the provided file extension. Args: file_extension (str): file extension Returns: osgeo.ogr.Driver Raises: ValueError: no driver is found """ filename, file_extension = os.path.splitext(filepath) EXTENSION = file_extension[1:] ogr_driver_count = ogr.GetDriverCount() for idx in range(ogr_driver_count): driver = ogr.GetDriver(idx) driver_extension = driver.GetMetadataItem(str('DMD_EXTENSION')) or '' driver_extensions = driver.GetMetadataItem(str('DMD_EXTENSIONS')) or '' if EXTENSION == driver_extension or EXTENSION in driver_extensions: return driver else: msg = 'No driver found for the following file extension: {}'.format( EXTENSION) raise ValueError(msg)
def print_drivers(): """Print a list of available drivers.""" for i in range(ogr.GetDriverCount()): driver = ogr.GetDriver(i) writeable = driver.TestCapability(ogr.ODrCCreateDataSource) print("{0} ({1})".format(driver.GetName(), "read/write" if writeable else "readonly"))
def draw_airspace(pszDataSource): # -------------------------------------------------------------------- # Open data source. # -------------------------------------------------------------------- poDS = None poDriver = None poDS = ogr.Open(pszDataSource) # -------------------------------------------------------------------- # Report failure. # -------------------------------------------------------------------- if poDS is None: print("FAILURE:\n" "Unable to open datasource `%s' with the following drivers." % pszDataSource) for iDriver in range(ogr.GetDriverCount()): print(" -> %s" % ogr.GetDriver(iDriver).GetName()) return 1 poDriver = poDS.GetDriver() # -------------------------------------------------------------------- # Some information messages. # -------------------------------------------------------------------- print("INFO: Open of `%s'\n" " using driver `%s' successful." % (pszDataSource, poDriver.GetName())) poDS_Name = poDS.GetName() if str(type(pszDataSource)) == "<type 'unicode'>" and str( type(poDS_Name)) == "<type 'str'>": poDS_Name = poDS_Name.decode("utf8") if pszDataSource != poDS_Name: print("INFO: Internal data source name `%s'\n" " different from user name `%s'." % (poDS_Name, pszDataSource)) #gdal.Debug( "OGR", "GetLayerCount() = %d\n", poDS.GetLayerCount() ) # -------------------------------------------------------------------- # Process specified data source layers. # -------------------------------------------------------------------- poLayer = poDS.GetLayerByName("airspaces") if poLayer is None: print("FAILURE: Couldn't fetch requested layer %s!" % papszIter) return 1 ReadLayer(poLayer) # -------------------------------------------------------------------- # Close down. # -------------------------------------------------------------------- poDS.Destroy() for airspace in airspaces: airspace.draw()
def drivers_ogr_gdal_disponibles(): """ Retorna lista de drivers disponibles a través de la librería osgeo-gdal-ogr Returns: dict """ cnt = ogr.GetDriverCount() driver_list = [] drivers = OrderedDict() for i in range(cnt): driver = ogr.GetDriver(i) driver_name = driver.GetName() driver_list.append(driver_name) for driver_name in driver_list: # Is File GeoDatabase available? drv = ogr.GetDriverByName(driver_name) if drv is None: print_warning("{} !!ATENTION - driver NOT available!!".format(driver_name)) else: drivers[driver_name] = drv print_debug(driver_name) return drivers
def _get_supported_extensions(type_): """Retrieves supported raster file extensions from gdal according to installed drivers. Arguments: type_ (str): 'raster' or 'vector' Returns: (tuple): Lists with supported file extensions. """ from osgeo import gdal, ogr primary = [] exts = [] if type_ == "raster": driver_count = gdal.GetDriverCount() dcap = gdal.DCAP_RASTER elif type_ == "vector": driver_count = ogr.GetDriverCount() dcap = gdal.DCAP_VECTOR else: raise ValueError("`type_` should be 'raster' or 'vector'") for i in range(driver_count): drv = gdal.GetDriver(i) if type_ == "raster" else ogr.GetDriver(i) if drv.GetMetadataItem(dcap): ext = drv.GetMetadataItem(gdal.DMD_EXTENSIONS) if ext is None or ext == '': continue ext = ext.split(' ') primary.append(ext[0]) exts.extend(ext) if type_ == "vector" and 'tsv' not in exts: exts.append('tsv') return (primary, exts)
def shapefile_engine_prepare(self): log.info('Preparing shapefiles engine...') # OGR provides dozens of drivers to read different types of file formats. # But I can't find the documention (list of drivers) online. So just # snoop. # FIXME: Do this just if debugging. for driver_i in xrange(ogr.GetDriverCount()): log.verbose(' >> ogr driver[%d]: %s' % ( driver_i, ogr.GetDriver(driver_i).GetName(), )) # We want the shapefile driver, which is also the first one in the list. self.file_driver = ogr.GetDriverByName('ESRI Shapefile') # Make the '.out' and '.fin' directories. opath = self.mjob.make_working_directories() # The datasource is just another directory, and each layer is saved as a # Shapefile, so it doesn't make sense to let the user have much # influence over what we name these files. # Make the folder name the same as the zip we'll create, # e.g., "Cyclopath-Export". datasource_name = self.mjob.wtem.get_zipname() spath = os.path.join(opath, datasource_name) log.debug('shapefile_engine_prepare: spath:') log.debug('%s' % (spath, )) self.outp_datasrc = self.file_driver.CreateDataSource(spath) # 2014.09.04: chmod, lest: `ls -l ./...out`: # drwxr-s--- 2 www-data www-data 4.0K 2014-08-19 13:14 Cyclopath-Export/ os.chmod(spath, 02775) self.spat_ref = osr.SpatialReference() # This is wrong: # self.spat_ref.SetWellKnownGeogCS('WGS84') # This just writes some meta info: # self.spat_ref.SetWellKnownGeogCS('NAD83') # But this writes just the basics: # nZone = 15 # bNorth = True # self.spat_ref.SetUTM(nZone, bNorth) # i.e., the basics are: # PROJECTION["Transverse_Mercator"], # PARAMETER["latitude_of_origin",0], # PARAMETER["central_meridian",-93], # PARAMETER["scale_factor",0.9996], # PARAMETER["false_easting",500000], # PARAMETER["false_northing",0], # UNIT["Meter",1]] # The documentation says this should work but I get nothing: # self.spat_ref.SetWellKnownGeogCS('EPSG:26915') # But I found that this works: # self.spat_ref.ImportFromEPSG(26915) self.spat_ref.ImportFromEPSG(conf.default_srid)
def Usage(): print( "Usage: ogr2ogr [--help-general] [-skipfailures] [-append] [-update] [-gt n]\n" + \ " [-select field_list] [-where restricted_where] \n" + \ " [-progress] [-sql <sql statement>] \n" + \ " [-spat xmin ymin xmax ymax] [-preserve_fid] [-fid FID]\n" + \ " [-a_srs srs_def] [-t_srs srs_def] [-s_srs srs_def]\n" + \ " [-f format_name] [-overwrite] [[-dsco NAME=VALUE] ...]\n" + \ " [-simplify tolerance]\n" + \ #// " [-segmentize max_dist] [-fieldTypeToString All|(type1[,type2]*)]\n" + \ " [-fieldTypeToString All|(type1[,type2]*)] [-explodecollections] \n" + \ " dst_datasource_name src_datasource_name\n" + \ " [-lco NAME=VALUE] [-nln name] [-nlt type] [layer [layer ...]]\n" + \ "\n" + \ " -f format_name: output file format name, possible values are:") for iDriver in range(ogr.GetDriverCount()): poDriver = ogr.GetDriver(iDriver) if poDriver.TestCapability(ogr.ODrCCreateDataSource): print(" -f \"" + poDriver.GetName() + "\"") print( " -append: Append to existing layer instead of creating new if it exists\n" + \ " -overwrite: delete the output layer and recreate it empty\n" + \ " -update: Open existing output datasource in update mode\n" + \ " -progress: Display progress on terminal. Only works if input layers have the \"fast feature count\" capability\n" + \ " -select field_list: Comma-delimited list of fields from input layer to\n" + \ " copy to the new layer (defaults to all)\n" + \ " -where restricted_where: Attribute query (like SQL WHERE)\n" + \ " -sql statement: Execute given SQL statement and save result.\n" + \ " -skipfailures: skip features or layers that fail to convert\n" + \ " -gt n: group n features per transaction (default 200)\n" + \ " -spat xmin ymin xmax ymax: spatial query extents\n" + \ " -simplify tolerance: distance tolerance for simplification.\n" + \ #//" -segmentize max_dist: maximum distance between 2 nodes.\n" + \ #//" Used to create intermediate points\n" + \ " -dsco NAME=VALUE: Dataset creation option (format specific)\n" + \ " -lco NAME=VALUE: Layer creation option (format specific)\n" + \ " -nln name: Assign an alternate name to the new layer\n" + \ " -nlt type: Force a geometry type for new layer. One of NONE, GEOMETRY,\n" + \ " POINT, LINESTRING, POLYGON, GEOMETRYCOLLECTION, MULTIPOINT,\n" + \ " MULTIPOLYGON, or MULTILINESTRING. Add \"25D\" for 3D layers.\n" + \ " Default is type of source layer.\n" + \ " -fieldTypeToString type1,...: Converts fields of specified types to\n" + \ " fields of type string in the new layer. Valid types are : \n" + \ " Integer, Real, String, Date, Time, DateTime, Binary, IntegerList, RealList,\n" + \ " StringList. Special value All can be used to convert all fields to strings.") print(" -a_srs srs_def: Assign an output SRS\n" + \ " -t_srs srs_def: Reproject/transform to this SRS on output\n" + \ " -s_srs srs_def: Override source SRS\n" + \ "\n" + \ " Srs_def can be a full WKT definition (hard to escape properly),\n" + \ " or a well known definition (ie. EPSG:4326) or a file with a WKT\n" + \ " definition." ) return False
def listDriver(self, context): formatsList = [] cnt = ogr.GetDriverCount() for i in range(cnt): driver = ogr.GetDriver(i) name = driver.GetName() description = driver.GetDescription() if not name in formatsList: formatsList.append( (name, description, '') ) formatsList.sort() return formatsList
def get_all_drivers(): """ get all the drivers GDAL supports :return: """ driver_dic = [] cnt = ogr.GetDriverCount() for i in range(cnt): driver_name = ogr.GetDriver(i).GetName() if not driver_name in driver_dic: driver_dic.append(driver_name) print(driver_dic)
def __init__(self, infile, verbose=False): ''' create a object with the source file (infile = geodata source) ''' supportedformats = [] self.sr_wgs84 = osr.SpatialReference() self.sr_wgs84.ImportFromEPSG(4326) driver = None for g in range(0, ogr.GetDriverCount()): supportedformats.append(ogr.GetDriver(g).GetName()) driver = ogr.GetDriver(g) datasource = driver.Open(infile) if datasource != None: self.datasource = datasource self.formatfound = ogr.GetDriver(g).GetName() break if datasource is None: message = 'Could not find the driver for %s' % infile if verbose: message += "\n" message += "List of supported drivers:" for sf in supportedformats: message += "- %s\n" % sf raise Exception, message self.layers_fields = [] for idx in range(0, datasource.GetLayerCount()): layer = datasource.GetLayer(idx) sr = layer.GetSpatialRef() if sr is None: raise Exception, 'Projection not present' fields = {} self.geomtypes.append(layer.GetGeomType()) for s in layer.schema: print 'FOUND SCHEMA FIELD', s.GetName() fields[s.GetName().decode('latin-1')] = s.GetTypeName() self.layers_fields.append(fields) layer.ResetReading()
def ogr_db2_listdrivers(): cnt = ogr.GetDriverCount() formatsList = [] # Empty List for i in range(cnt): driver = ogr.GetDriver(i) driverName = driver.GetName() # print driverName if driverName not in formatsList: formatsList.append(driverName) formatsList.sort() # Sorting the messy list of ogr drivers for i in formatsList: print(i)
def list_drivers(): # list available drivers (i.e., readers for different file formats) cnt = ogr.GetDriverCount() formatsList = [] # Empty List for i in range(cnt): driver = ogr.GetDriver(i) driverName = driver.GetName() if not driverName in formatsList: formatsList.append(driverName) formatsList.sort() # Sorting the messy list of ogr drivers for i in formatsList: print(i)
def ogr_drivers(): ''' This coude returns the list of OGR drivers alphabetically from A-Z ''' cnt = ogr.GetDriverCount() formats_list = [] for i in range(cnt): driver = ogr.GetDriver(i) driver_name = driver.GetName() if not driver_name in formats_list: formats_list.append(driver_name) formats_list.sort() for i in formats_list: print i print "The number of ogr drivers is %d" % (cnt)
def list_formats(): cnt = ogr.GetDriverCount() formatsList = [] for i in range(cnt): driver = ogr.GetDriver(i) if not driver.TestCapability("CreateDataSource"): continue driverName = driver.GetName() if driverName == 'GML': continue formatsList.append(driverName.replace(' ', '_')) for i in sorted(formatsList): print(i)
def list_ogr_drivers(): """ Provide a list with all the available OGR drivers """ cnt = ogr.GetDriverCount() driver_list = [] for i in range(cnt): driver = ogr.GetDriver(i) driver_name = driver.GetName() if driver_name not in driver_list: driver_list.append(driver_name) driver_list.sort() # Sorting the messy list of ogr drivers for i, drv in enumerate(driver_list): print("%3s: %25s" % (i, drv))
def ListOGR_drivers(): """ Function to print out a list of all available drivers for OGR Returns: N/A """ cnt = ogr.GetDriverCount() formatsList = [] # Empty List for i in range(cnt): driver = ogr.GetDriver(i) driverName = driver.GetName() if not driverName in formatsList: formatsList.append(driverName) formatsList.sort() # Sorting the messy list of ogr drivers for i in formatsList: print(i)
def get_ogr_driver(filepath): """Get the OGR driver based on the file's extension. :param filepath: file's path :type filepath: str :raises UnsupportedVectorType: unsupported extension :return: OGR driver :rtype: osgeo.ogr.Driver """ filename, file_extension = os.path.splitext(filepath) extension = file_extension[1:] ogr_driver_count = ogr.GetDriverCount() for idx in range(ogr_driver_count): driver = ogr.GetDriver(idx) driver_extension = driver.GetMetadataItem(str("DMD_EXTENSION")) or "" driver_extensions = driver.GetMetadataItem(str("DMD_EXTENSIONS")) or "" if extension == driver_extension or extension in driver_extensions: return driver raise UnsupportedVectorType
def driverList(drivertype='raster'): '''Return the list of available GDAL/OGR drivers''' if not drivertype: types = ['gdal'] elif isinstance(drivertype, string_types): types = [drivertype] else: types = drivertype if not set(('raster', 'vector')).issuperset(types): raise ValueError('invalid type list: "%s"' % types) drivers = [] if 'raster' in types: drivers.extend( gdal.GetDriver(index) for index in range(gdal.GetDriverCount())) if 'vector' in types: # @TODO: check from osgeo import ogr drivers.extend( ogr.GetDriver(index) for index in range(ogr.GetDriverCount())) return drivers
from osgeo import ogr cnt = ogr.GetDriverCount() for i in range(cnt): print(ogr.GetDriver(i).GetName())
def main(argv=None): global bReadOnly global bVerbose global bSummaryOnly global nFetchFID global papszOptions pszWHERE = None pszDataSource = None papszLayers = None poSpatialFilter = None nRepeatCount = 1 bAllLayers = False pszSQLStatement = None pszDialect = None options = {} pszGeomField = None if argv is None: argv = sys.argv argv = ogr.GeneralCmdLineProcessor(argv) # -------------------------------------------------------------------- # Processing command line arguments. # -------------------------------------------------------------------- if argv is None: return 1 nArgc = len(argv) iArg = 1 while iArg < nArgc: if EQUAL(argv[iArg], "--utility_version"): print("%s is running against GDAL %s" % (argv[0], gdal.VersionInfo("RELEASE_NAME"))) return 0 elif EQUAL(argv[iArg], "-ro"): bReadOnly = True elif EQUAL(argv[iArg], "-q") or EQUAL(argv[iArg], "-quiet"): bVerbose = False elif EQUAL(argv[iArg], "-fid") and iArg < nArgc - 1: iArg = iArg + 1 nFetchFID = int(argv[iArg]) elif EQUAL(argv[iArg], "-spat") and iArg + 4 < nArgc: oRing = ogr.Geometry(ogr.wkbLinearRing) oRing.AddPoint(float(argv[iArg + 1]), float(argv[iArg + 2])) oRing.AddPoint(float(argv[iArg + 1]), float(argv[iArg + 4])) oRing.AddPoint(float(argv[iArg + 3]), float(argv[iArg + 4])) oRing.AddPoint(float(argv[iArg + 3]), float(argv[iArg + 2])) oRing.AddPoint(float(argv[iArg + 1]), float(argv[iArg + 2])) poSpatialFilter = ogr.Geometry(ogr.wkbPolygon) poSpatialFilter.AddGeometry(oRing) iArg = iArg + 4 elif EQUAL(argv[iArg], "-geomfield") and iArg < nArgc - 1: iArg = iArg + 1 pszGeomField = argv[iArg] elif EQUAL(argv[iArg], "-where") and iArg < nArgc - 1: iArg = iArg + 1 pszWHERE = argv[iArg] elif EQUAL(argv[iArg], "-sql") and iArg < nArgc - 1: iArg = iArg + 1 pszSQLStatement = argv[iArg] elif EQUAL(argv[iArg], "-dialect") and iArg < nArgc - 1: iArg = iArg + 1 pszDialect = argv[iArg] elif EQUAL(argv[iArg], "-rc") and iArg < nArgc - 1: iArg = iArg + 1 nRepeatCount = int(argv[iArg]) elif EQUAL(argv[iArg], "-al"): bAllLayers = True elif EQUAL(argv[iArg], "-so") or EQUAL(argv[iArg], "-summary"): bSummaryOnly = True elif len(argv[iArg]) > 8 and EQUAL(argv[iArg][0:8], "-fields="): options['DISPLAY_FIELDS'] = argv[iArg][7:len(argv[iArg])] elif len(argv[iArg]) > 6 and EQUAL(argv[iArg][0:6], "-geom="): options['DISPLAY_GEOMETRY'] = argv[iArg][6:len(argv[iArg])] elif argv[iArg][0] == '-': return Usage() elif pszDataSource is None: pszDataSource = argv[iArg] else: if papszLayers is None: papszLayers = [] papszLayers.append(argv[iArg]) bAllLayers = False iArg = iArg + 1 if pszDataSource is None: return Usage() # -------------------------------------------------------------------- # Open data source. # -------------------------------------------------------------------- poDS = None poDriver = None poDS = ogr.Open(pszDataSource, not bReadOnly) if poDS is None and not bReadOnly: poDS = ogr.Open(pszDataSource, False) if poDS is not None and bVerbose: print("Had to open data source read-only.") bReadOnly = True # -------------------------------------------------------------------- # Report failure. # -------------------------------------------------------------------- if poDS is None: print("FAILURE:\n" "Unable to open datasource `%s' with the following drivers." % pszDataSource) for iDriver in range(ogr.GetDriverCount()): print(" -> %s" % ogr.GetDriver(iDriver).GetName()) return 1 poDriver = poDS.GetDriver() # -------------------------------------------------------------------- # Some information messages. # -------------------------------------------------------------------- if bVerbose: print("INFO: Open of `%s'\n" " using driver `%s' successful." % (pszDataSource, poDriver.GetName())) poDS_Name = poDS.GetName() if str(type(pszDataSource)) == "<type 'unicode'>" and str( type(poDS_Name)) == "<type 'str'>": poDS_Name = poDS_Name.decode("utf8") if bVerbose and pszDataSource != poDS_Name: print("INFO: Internal data source name `%s'\n" " different from user name `%s'." % (poDS_Name, pszDataSource)) # -------------------------------------------------------------------- # Special case for -sql clause. No source layers required. # -------------------------------------------------------------------- if pszSQLStatement is not None: poResultSet = None nRepeatCount = 0 #// skip layer reporting. if papszLayers is not None: print("layer names ignored in combination with -sql.") if pszGeomField is None: poResultSet = poDS.ExecuteSQL(pszSQLStatement, poSpatialFilter, pszDialect) else: poResultSet = poDS.ExecuteSQL(pszSQLStatement, None, pszDialect) if poResultSet is not None: if pszWHERE is not None: if poResultSet.SetAttributeFilter(pszWHERE) != 0: print("FAILURE: SetAttributeFilter(%s) failed." % pszWHERE) return 1 if pszGeomField is not None: ReportOnLayer(poResultSet, None, pszGeomField, poSpatialFilter, options) else: ReportOnLayer(poResultSet, None, None, None, options) poDS.ReleaseResultSet(poResultSet) #gdal.Debug( "OGR", "GetLayerCount() = %d\n", poDS.GetLayerCount() ) for iRepeat in range(nRepeatCount): if papszLayers is None: # -------------------------------------------------------------------- # Process each data source layer. # -------------------------------------------------------------------- for iLayer in range(poDS.GetLayerCount()): poLayer = poDS.GetLayer(iLayer) if poLayer is None: print("FAILURE: Couldn't fetch advertised layer %d!" % iLayer) return 1 if not bAllLayers: line = "%d: %s" % (iLayer + 1, poLayer.GetLayerDefn().GetName()) nGeomFieldCount = poLayer.GetLayerDefn().GetGeomFieldCount( ) if nGeomFieldCount > 1: line = line + " (" for iGeom in range(nGeomFieldCount): if iGeom > 0: line = line + ", " poGFldDefn = poLayer.GetLayerDefn( ).GetGeomFieldDefn(iGeom) line = line + "%s" % ogr.GeometryTypeToName( poGFldDefn.GetType()) line = line + ")" if poLayer.GetLayerDefn().GetGeomType() != ogr.wkbUnknown: line = line + " (%s)" % ogr.GeometryTypeToName( poLayer.GetLayerDefn().GetGeomType()) print(line) else: if iRepeat != 0: poLayer.ResetReading() ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, options) else: # -------------------------------------------------------------------- # Process specified data source layers. # -------------------------------------------------------------------- for papszIter in papszLayers: poLayer = poDS.GetLayerByName(papszIter) if poLayer is None: print("FAILURE: Couldn't fetch requested layer %s!" % papszIter) return 1 if iRepeat != 0: poLayer.ResetReading() ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, options) # -------------------------------------------------------------------- # Close down. # -------------------------------------------------------------------- poDS.Destroy() return 0
def GetFeature(self, request, kwargs): """ """ mimetypes = {'GeoJSON': 'application/json'} if 'xml' in kwargs: parms = self._parse_xml_GetFeature(kwargs['xml']) else: parms = GetFeatureMixin.Parameters.create(kwargs) # must be an OGR dataset or a QuerySet containing one layer response = self.adapter.get_features(request, parms) if isinstance(response, GeoQuerySet): layer = None db_params = settings.DATABASES[response.db] if db_params['ENGINE'].endswith('postgis'): # Then we take the raw SQL from thr QuerySet and pass it through OGR instead. This causes the SQL to be # executed twice, but it's also the most expedient way to create our output. This could be done better, # but it gets it out the door for now. # Create the query from the QuerySet # adapt() prevents SQL injection attacks from psycopg2.extensions import adapt query, parameters = response.query.get_compiler( response.db).as_sql() parameters = tuple([adapt(p) for p in parameters]) query = query % parameters # Connect to PostGIS with OGR. drv = ogr.GetDriverByName("PostgreSQL") connection_string = "PG:dbname='{db}'".format( db=db_params['NAME']) if 'HOST' in db_params and db_params['HOST']: connection_string += " host='{host}'".format( host=db_params['HOST']) if 'PORT' in db_params and db_params['PORT']: connection_string += " port='{port}'".format( port=db_params['PORT']) if 'USER' in db_params and db_params['USER']: connection_string += " user='******'".format( user=db_params['USER']) if 'PASSWORD' in db_params and db_params['PASSWORD']: connection_string += " password='******'".format( password=db_params['PASSWORD']) conn = drv.Open(connection_string) # Put the QuerySet into a layer the hard way. layer = conn.ExecuteSQL(query.encode('ascii')) elif db_params['ENGINE'].endswith('spatialite'): # This works the same way as the if-statement above. # todo replace this with the sqlite version of the same thing for preventing SQL injection attacks from psycopg2.extensions import adapt query, parameters = response.query.get_compiler( response.db).as_sql() parameters = tuple([adapt(p) for p in parameters]) query = query % parameters drv = ogr.GetDriverByName("Spatialite") conn = drv.Open(db_params['NAME']) layer = conn.ExecuteSQL(query) else: layer = response.GetLayerByIndex(0) drivers = dict([ (ogr.GetDriver(drv).GetName(), ogr.GetDriver(drv)) for drv in range(ogr.GetDriverCount()) if ogr.GetDriver(drv).TestCapability(ogr.ODrCCreateDataSource) ]) output_format = parms.cleaned_data['output_format'].decode('ascii') if 'gml' in output_format or 'xml' in output_format: tmpname = "{tmpdir}{sep}{uuid}.{output_format}".format( tmpdir=gettempdir(), uuid=uuid4(), output_format='gml', sep=os.path.sep) drv = ogr.GetDriverByName("GML") ds = drv.CreateDataSource(tmpname) l2 = ds.CopyLayer(layer, 'WFS_result') l2.SyncToDisk() del ds responsef = open(tmpname) rdata = responsef.read() responsef.close() os.unlink(tmpname) return HttpResponse(rdata, mimetype=output_format) elif output_format in drivers: tmpname = "{tmpdir}{sep}{uuid}.{output_format}".format( tmpdir=gettempdir(), uuid=uuid4(), output_format=output_format, sep=os.path.sep) drv = drivers[output_format] ds = drv.CreateDataSource(tmpname) l2 = ds.CopyLayer(layer, 'WFS_result') l2.SyncToDisk() del ds responsef = open(tmpname) rdata = responsef.read() responsef.close() os.unlink(tmpname) return HttpResponse(rdata, mimetype=mimetypes.get(output_format, 'text/plain')) else: raise OperationProcessingFailed.at( 'GetFeature', 'outputFormat {of} not supported ({formats})'.format( of=output_format, formats=drivers.keys()))
from osgeo import gdal from osgeo import ogr from osgeo import osr from .. import geoprocessing from ..geoprocessing_core import DEFAULT_GTIFF_CREATION_TUPLE_OPTIONS LOGGER = logging.getLogger('pygeoprocessing.testing.sampledata') ReferenceData = collections.namedtuple('ReferenceData', 'projection origin pixel_size') gdal.AllRegister() GDAL_DRIVERS = sorted([ gdal.GetDriver(i).GetDescription() for i in range(1, gdal.GetDriverCount()) ]) OGR_DRIVERS = sorted( [ogr.GetDriver(i).GetName() for i in range(ogr.GetDriverCount())]) # Mappings of numpy -> GDAL types and GDAL -> numpy types. NUMPY_GDAL_DTYPES = { numpy.byte: gdal.GDT_Byte, numpy.ubyte: gdal.GDT_Byte, numpy.uint16: gdal.GDT_UInt16, numpy.uint32: gdal.GDT_UInt32, numpy.int16: gdal.GDT_Int16, numpy.int32: gdal.GDT_Int32, numpy.int64: gdal.GDT_Int32, # GDAL doesn't have an int6. numpy.float32: gdal.GDT_Float32, numpy.float64: gdal.GDT_Float64, numpy.cfloat: gdal.GDT_CFloat32, numpy.cfloat: gdal.GDT_CFloat64 }
def vectInfo(conf,inputs,outputs): global bReadOnly global bVerbose global bSummaryOnly global nFetchFID global papszOptions pszWHERE = None pszDataSource = None papszLayers = None poSpatialFilter = None nRepeatCount = 1 bAllLayers = False pszSQLStatement = None pszDialect = None options = {} pszSQLStatement = inputs["q"]["value"] pszDataSource = inputs["dstName"]["value"] #papszLayers.append(inputs["dsoName"]["value"]) #/* -------------------------------------------------------------------- */ #/* Open data source. */ #/* -------------------------------------------------------------------- */ poDS = None poDriver = None poDS = ogr.Open( pszDataSource, not bReadOnly ) if poDS is None and not bReadOnly: poDS = ogr.Open( pszDataSource, False ) if poDS is not None and bVerbose: print >> sys.stderr,"Had to open data source read-only." bReadOnly = True #/* -------------------------------------------------------------------- */ #/* Report failure */ #/* -------------------------------------------------------------------- */ if poDS is None: print >>sys.stderr, "FAILURE:\n"\ "Unable to open datasource `%s' with the following drivers." % pszDataSource conf["lenv"]["message"]="FAILURE:\n"\ "Unable to open datasource `%s' with the following drivers." % pszDataSource for iDriver in range(ogr.GetDriverCount()): conf["lenv"]["message"]+=" -> %s" % ogr.GetDriver(iDriver).GetName() return 4 poDriver = poDS.GetDriver() poDS_Name = poDS.GetName() if str(type(pszDataSource)) == "<type 'unicode'>" and str(type(poDS_Name)) == "<type 'str'>": poDS_Name = unicode(poDS_Name, "utf8") #/* -------------------------------------------------------------------- */ #/* Special case for -sql clause. No source layers required. */ #/* -------------------------------------------------------------------- */ if pszSQLStatement is not None: poResultSet = None nRepeatCount = 0 #// skip layer reporting. if papszLayers is not None: print >> sys.stderr, "layer names ignored in combination with -sql." poResultSet = poDS.ExecuteSQL( pszSQLStatement, poSpatialFilter, pszDialect ) if poResultSet is not None: if pszWHERE is not None: poResultSet.SetAttributeFilter( pszWHERE ) res=[] ReportOnLayer( inputs, res, poResultSet, None, None, options ) outputs["Result"]["value"]=json.dumps(res,ensure_ascii=False) poDS.ReleaseResultSet( poResultSet ) else: for iRepeat in range(nRepeatCount): if papszLayers is None: #/* -------------------------------------------------------------------- */ #/* Process each data source layer. */ #/* -------------------------------------------------------------------- */ for iLayer in range(poDS.GetLayerCount()): poLayer = poDS.GetLayer(iLayer) if poLayer is None: print( "FAILURE: Couldn't fetch advertised layer %d!" % iLayer ) return 1 if not bAllLayers: line = "%d: %s" % (iLayer+1, poLayer.GetLayerDefn().GetName()) if poLayer.GetLayerDefn().GetGeomType() != ogr.wkbUnknown: line = line + " (%s)" % ogr.GeometryTypeToName( poLayer.GetLayerDefn().GetGeomType() ) print(line) else: if iRepeat != 0: poLayer.ResetReading() res=[] ReportOnLayer( inputs, res, poLayer, pszWHERE, poSpatialFilter, options ) outputs["Result"]["value"]=json.dumps(res,ensure_ascii=False) else: #/* -------------------------------------------------------------------- */ #/* Process specified data source layers. */ #/* -------------------------------------------------------------------- */ for papszIter in papszLayers: poLayer = poDS.GetLayerByName(papszIter) if poLayer is None: print( "FAILURE: Couldn't fetch requested layer %s!" % papszIter ) return 1 if iRepeat != 0: poLayer.ResetReading() res=[] ReportOnLayer( inputs, res, poLayer, pszWHERE, poSpatialFilter, options ) outputs["Result"]["value"]=json.dumps(res,ensure_ascii=False) #/* -------------------------------------------------------------------- */ #/* Close down. */ #/* -------------------------------------------------------------------- */ poDS.Destroy() #import json #print >> sys.stderr,outputs["Result"]["value"] #outputs["Result"]["value"]=str(outputs["Result"]["value"]) return 3
def ogr_formats(): for iDriver in range(ogr.GetDriverCount()): print(" -> %s" % ogr.GetDriver(iDriver).GetName())
# https://pcjericks.github.io/py-gdalogr-cookbook/ import sys try: from osgeo import ogr, osr, gdal print("GDAL VERSION: {}".format(gdal.__version__)) gdal_driver_list = [ gdal.GetDriver(i).GetDescription() for i in range(gdal.GetDriverCount()) ] ogr_driver_list = [ ogr.GetDriver(i).GetDescription() for i in range(ogr.GetDriverCount()) ] print("\nGDAL DRIVERS:") print(gdal_driver_list) print("\nOGR DRIVERS:") print(ogr_driver_list) except: sys.exit('ERROR: cannot find GDAL/OGR modules')
def ogrinfo(readonly=False, verbose=True, summaryonly=False, fetch_fid=ogr.NullFID, where=None, datasource_name=None, layers=None, spatial_filter=None, repeat_count=1, all_layers=False, sql_statement=None, dialect=None, options={}, geomfield=None): global bVerbose global bSummaryOnly global nFetchFID bReadOnly = readonly bVerbose = verbose bSummaryOnly = summaryonly nFetchFID = fetch_fid pszWHERE = where pszDataSource = datasource_name bReadOnly = readonly papszLayers = layers poSpatialFilter = spatial_filter nRepeatCount = repeat_count bAllLayers = all_layers pszSQLStatement = sql_statement pszDialect = dialect pszGeomField = geomfield #/* -------------------------------------------------------------------- */ #/* Open data source. */ #/* -------------------------------------------------------------------- */ poDS = None poDriver = None poDS = ogr.Open(pszDataSource, not bReadOnly) if poDS is None and not bReadOnly: poDS = ogr.Open(pszDataSource, False) if poDS is not None and bVerbose: print("Had to open data source read-only.") bReadOnly = True #/* -------------------------------------------------------------------- */ #/* Report failure */ #/* -------------------------------------------------------------------- */ if poDS is None: print("FAILURE:\n" "Unable to open datasource `%s' with the following drivers." % pszDataSource) for iDriver in range(ogr.GetDriverCount()): print(" -> %s" % ogr.GetDriver(iDriver).GetName()) return 1 poDriver = poDS.GetDriver() #/* -------------------------------------------------------------------- */ #/* Some information messages. */ #/* -------------------------------------------------------------------- */ if bVerbose: print("INFO: Open of `%s'\n" " using driver `%s' successful." % (pszDataSource, poDriver.GetName())) poDS_Name = poDS.GetName() if str(type(pszDataSource)) == "<type 'unicode'>" and str( type(poDS_Name)) == "<type 'str'>": poDS_Name = str(poDS_Name, "utf8") if bVerbose and pszDataSource != poDS_Name: print("INFO: Internal data source name `%s'\n" " different from user name `%s'." % (poDS_Name, pszDataSource)) #/* -------------------------------------------------------------------- */ #/* Special case for -sql clause. No source layers required. */ #/* -------------------------------------------------------------------- */ if pszSQLStatement is not None: poResultSet = None nRepeatCount = 0 #// skip layer reporting. if papszLayers is not None: print("layer names ignored in combination with -sql.") if pszGeomField is None: poResultSet = poDS.ExecuteSQL(pszSQLStatement, poSpatialFilter, pszDialect) else: poResultSet = poDS.ExecuteSQL(pszSQLStatement, None, pszDialect) if poResultSet is not None: if pszWHERE is not None: if poResultSet.SetAttributeFilter(pszWHERE) != 0: print("FAILURE: SetAttributeFilter(%s) failed." % pszWHERE) return 1 if pszGeomField is not None: ReportOnLayer(poResultSet, None, pszGeomField, poSpatialFilter, options) else: ReportOnLayer(poResultSet, None, None, None, options) poDS.ReleaseResultSet(poResultSet) #gdal.Debug( "OGR", "GetLayerCount() = %d\n", poDS.GetLayerCount() ) for iRepeat in range(nRepeatCount): if papszLayers is None: #/* -------------------------------------------------------------------- */ #/* Process each data source layer. */ #/* -------------------------------------------------------------------- */ for iLayer in range(poDS.GetLayerCount()): poLayer = poDS.GetLayer(iLayer) if poLayer is None: print("FAILURE: Couldn't fetch advertised layer %d!" % iLayer) return 1 if not bAllLayers: line = "%d: %s" % (iLayer + 1, poLayer.GetLayerDefn().GetName()) nGeomFieldCount = poLayer.GetLayerDefn().GetGeomFieldCount( ) if nGeomFieldCount > 1: line = line + " (" for iGeom in range(nGeomFieldCount): if iGeom > 0: line = line + ", " poGFldDefn = poLayer.GetLayerDefn( ).GetGeomFieldDefn(iGeom) line = line + "%s" % ogr.GeometryTypeToName( poGFldDefn.GetType()) line = line + ")" if poLayer.GetLayerDefn().GetGeomType() != ogr.wkbUnknown: line = line + " (%s)" % ogr.GeometryTypeToName( poLayer.GetLayerDefn().GetGeomType()) print(line) else: if iRepeat != 0: poLayer.ResetReading() ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, options) else: #/* -------------------------------------------------------------------- */ #/* Process specified data source layers. */ #/* -------------------------------------------------------------------- */ for papszIter in papszLayers: poLayer = poDS.GetLayerByName(papszIter) if poLayer is None: print("FAILURE: Couldn't fetch requested layer %s!" % papszIter) return 1 if iRepeat != 0: poLayer.ResetReading() ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, options) #/* -------------------------------------------------------------------- */ #/* Close down. */ #/* -------------------------------------------------------------------- */ poDS.Destroy() return 0
def getSupportedVectors(self): if self.supportedVectors != None: return self.supportedVectors # first get the OGR driver manager QgsApplication.registerOgrDrivers() self.supportedVectors = dict() # for each loaded OGR driver for i in range(ogr.GetDriverCount()): driver = ogr.GetDriver(i) if driver == None: QgsLogger.warning("unable to get driver " + str(i)) continue driverName = driver.GetName() longName = '' glob = [] if driverName.startswith("AVCBin"): pass #myDirectoryDrivers += "Arc/Info Binary Coverage,AVCBin" elif driverName.startswith("AVCE00"): longName = "Arc/Info ASCII Coverage" glob.append("*.e00") elif driverName.startswith("BNA"): longName = "Atlas BNA" glob.append("*.bna") elif driverName.startswith("CSV"): longName = "Comma Separated Value" glob.append("*.csv") elif driverName.startswith("DODS"): pass #myProtocolDrivers += "DODS/OPeNDAP,DODS" elif driverName.startswith("PGeo"): pass #myDatabaseDrivers += "ESRI Personal GeoDatabase,PGeo" # on Windows add a pair to the dict for this driver if platform.system() == "Windows": longName = "ESRI Personal GeoDatabase" glob.append("*.mdb") elif driverName.startswith("SDE"): pass #myDatabaseDrivers += "ESRI ArcSDE,SDE" elif driverName.startswith("ESRI"): longName = "ESRI Shapefiles" glob.append("*.shp") elif driverName.startswith("FMEObjects Gateway"): longName = "FMEObjects Gateway" glob.append("*.fdd") elif driverName.startswith("GeoJSON"): pass #myProtocolDrivers += "GeoJSON,GeoJSON" longName = "GeoJSON" glob.append("*.geojson") elif driverName.startswith("GeoRSS"): longName = "GeoRSS" glob.append("*.xml") elif driverName.startswith("GML"): longName = "Geography Markup Language" glob.append("*.gml") elif driverName.startswith("GMT"): longName = "GMT" glob.append("*.gmt") elif driverName.startswith("GPX"): longName = "GPX" glob.append("*.gpx") elif driverName.startswith("GRASS"): pass #myDirectoryDrivers += "Grass Vector,GRASS" elif driverName.startswith("IDB"): pass #myDatabaseDrivers += "Informix DataBlade,IDB" elif driverName.startswith("Interlis 1"): longName = "INTERLIS 1" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("Interlis 2"): longName = "INTERLIS 2" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("INGRES"): pass #myDatabaseDrivers += "INGRES,INGRES" elif driverName.startswith("KML"): longName = "KML" glob.append("*.kml") elif driverName.startswith("MapInfo File"): longName = "Mapinfo File" glob.append("*.mif") glob.append("*.tab") elif driverName.startswith("DGN"): longName = "Microstation DGN" glob.append("*.dgn") elif driverName.startswith("MySQL"): pass #myDatabaseDrivers += "MySQL,MySQL" elif driverName.startswith("OCI"): pass #myDatabaseDrivers += "Oracle Spatial,OCI" elif driverName.startswith("ODBC"): pass #myDatabaseDrivers += "ODBC,ODBC" elif driverName.startswith("OGDI"): pass #myDatabaseDrivers += "OGDI Vectors,OGDI" elif driverName.startswith("PostgreSQL"): pass #myDatabaseDrivers += "PostgreSQL,PostgreSQL" elif driverName.startswith("S57"): longName = "S-57 Base file" glob.append("*.000") elif driverName.startswith("SDTS"): longName = "Spatial Data Transfer Standard" glob.append("*catd.ddf") elif driverName.startswith("SQLite"): longName = "SQLite" glob.append("*.sqlite") elif driverName.startswith("UK .NTF"): pass #myDirectoryDrivers += "UK. NTF,UK. NTF" elif driverName.startswith("TIGER"): pass #myDirectoryDrivers += "U.S. Census TIGER/Line,TIGER" elif driverName.startswith("VRT"): longName = "VRT - Virtual Datasource " glob.append("*.vrt") elif driverName.startswith("XPlane"): longName = "X-Plane/Flighgear" glob.append("apt.dat") glob.append("nav.dat") glob.append("fix.dat") glob.append("awy.dat") longName = string.strip(longName) if longName == '': continue self.supportedVectors[longName] = { 'EXTENSIONS': glob, 'LONGNAME': longName, 'SHORTNAME': driverName } return self.supportedVectors
def drivers(self): list = [] if ogrAvailable: for iDriver in range(ogr.GetDriverCount()): list.append('%s' % ogr.GetDriver(iDriver).GetName()) return list