Ejemplo n.º 1
0
def cloud_poly(incld, outshp=None, polytype="ESRI Shapefile",
               reader="readers.las"):
    
    """
    Return the non-zero extent as a shapely polygon
    
    Parameters
    ----------
    
    incld: string
            input cloud
            
    outshp: string
            output file (optional)
    
    polytype: string
            the ogr term for the output polygon e.g. "ESRI Shapefile" (optional)
    
    Returns
    -------
    
    Shapely polygon
    
    """

    
    js = [incld, 
          {"type" : "filters.hexbin"}]
    
    pipeline = pdal.Pipeline(json.dumps(js))
    
    count = pipeline.execute()
    
    # get the json 
    meta = pipeline.metadata
    metajson = json.loads(meta)
    
    # in json is the polygon required
    boundarywkt = metajson['metadata']['filters.hexbin']['boundary']
    poly = loads(boundarywkt)
    
    if outshp != None:
        # this is a wkt
        spref = metajson['metadata'][reader]['spatialreference']
        
        proj = osr.SpatialReference()
        proj.ImportFromWkt(spref)
        
        out_drv = ogr.GetDriverByName(polytype)
        
        # remove output shapefile if it already exists
        if os.path.exists(outshp):
            out_drv.DeleteDataSource(outshp)
        
        # create the output shapefile
        ootds = out_drv.CreateDataSource(outshp)
        ootlyr = ootds.CreateLayer("extent", proj, geom_type=ogr.wkbPolygon)
        
        # add an ID field
        idField = ogr.FieldDefn("id", ogr.OFTInteger)
        ootlyr.CreateField(idField)
        
        # create the feature and set values
        featureDefn = ootlyr.GetLayerDefn()
        feature = ogr.Feature(featureDefn)
        # from shapely wkt export to ogr
        polyogr = ogr.CreateGeometryFromWkt(poly.wkt)
        feature.SetGeometry(polyogr)
        feature.SetField("id", 1)
        ootlyr.CreateFeature(feature)
        feature = None
        
        # Save and close 
        ootds.FlushCache()
        ootds = None
    
    return poly
Ejemplo n.º 2
0
def main(
    logfilename,
    destination,
    inputfile,
    projection,
    cellsize,
    locationid,
    snap=False,
    verbose=True,
):

    # open a logger, dependent on verbose print to screen or not
    logger, ch = wt.setlogger(logfilename, "WTOOLS", verbose)

    # delete old files
    if os.path.isdir(destination):
        shutil.rmtree(destination)
    os.makedirs(destination)

    ### Get information ####
    if inputfile is not None:
        # retrieve extent from input file. Check if projection is provided
        file_ext = os.path.splitext(os.path.basename(inputfile))[1]
        if file_ext in (".shp", ".geojson"):
            ds = ogr.Open(inputfile)
            # read the extent of the shapefile
            lyr = ds.GetLayer(0)
            extent = lyr.GetExtent()
            extent_in = [extent[0], extent[2], extent[1], extent[3]]
            # get spatial reference from shapefile
            srs = lyr.GetSpatialRef()
        else:
            # Read extent from a GDAL compatible file
            extent_in = wt.get_extent(inputfile)
            try:
                extent_in = wt.get_extent(inputfile)
            except:
                msg = "Input file {:s} not a shape or gdal file".format(
                    inputfile)
                wt.close_with_error(logger, ch, msg)
                sys.exit(1)

            # get spatial reference from grid file
            try:
                srs = wt.get_projection(inputfile)
            except:
                logger.warning(
                    "No projection found, assuming WGS 1984 lat long")
                srs = osr.SpatialReference()
                srs.ImportFromEPSG(4326)

        # geotransform = ds.GetGeoTransform()
        # raster_cellsize = geotransform[1]
        # ncols = ds.RasterXSize
        # nrows = ds.RasterYSize
        # extent_in = [geotransform[0],
        #              geotransform[3]-nrows*raster_cellsize,
        #              geotransform[0]+ncols*raster_cellsize,
        #              geotransform[3]]
        # # get spatial reference from grid file
        # WktString = ds.GetProjection()
        # srs = osr.SpatialReference()
        # srs.ImportFromWkt(WktString)
    else:
        lonmin, latmin, lonmax, latmax = extent
        srs_4326 = osr.SpatialReference()
        srs_4326.ImportFromEPSG(4326)
        srs = osr.SpatialReference()
        if projection is not None:
            # import projection as an srs object
            if projection.lower()[0:4] == "epsg":
                # make a proj4 string
                srs.ImportFromEPSG(int(projection[5:]))
            elif projection.lower()[0:5] == "+proj":
                srs.ImportFromProj4(projection)
            else:
                msg = 'Projection "{:s}" is not a valid projection'.format(
                    projection)
                wt.close_with_error(logger, ch, msg)
        else:
            logger.warning("No projection found, assuming WGS 1984 lat long")
            srs.ImportFromEPSG(4326)
        xmin, ymin = pyproj.transform(
            pyproj.Proj(srs_4326.ExportToProj4()),
            pyproj.Proj(srs.ExportToProj4()),
            lonmin,
            latmin,
        )
        xmax, ymax = pyproj.transform(
            pyproj.Proj(srs_4326.ExportToProj4()),
            pyproj.Proj(srs.ExportToProj4()),
            lonmax,
            latmax,
        )
        # project the extent parameters to selected projection and snap to
        # selected resolution
        extent_in = [xmin, ymin, xmax, ymax]

    # srs known, extent known, prepare UTM or WGS string for grid.xml
    logger.info('Projection "{:s}" used'.format(srs.ExportToProj4()))
    if srs.IsProjected():
        utm = srs.GetUTMZone()
        if utm < 0:
            hemisphere = "S"
        else:
            hemisphere = "N"
        geodatum = "UTM{:d}{:s}".format(np.abs(utm), hemisphere)
    else:
        geodatum = "WGS 1984"

    if snap:
        logger.info("Snapping raster")
        snap = len(str(cellsize - np.floor(cellsize))) - 2
        extent_out = wt.round_extent(extent_in, cellsize, snap)
    else:
        extent_out = extent_in
    cols = int((extent_out[2] - extent_out[0]) / cellsize)  # +2)
    rows = int((extent_out[3] - extent_out[1]) / cellsize)  # +2)
    cells = rows * cols
    xorg = extent_out[0]  # -cellsize
    yorg = extent_out[3]  # +cellsize

    # create clone raster
    print("rows: {0} cols: {1}".format(rows, cols))

    dummy_raster = np.zeros((rows, cols)) - 9999.
    clone_file_map = os.path.abspath(os.path.join(destination, "mask.map"))
    clone_file_tif = os.path.abspath(os.path.join(destination, "mask.tif"))
    logger.info("Writing PCRaster clone to {:s}".format(clone_file_map))
    wt.gdal_writemap(
        clone_file_map,
        "PCRaster",
        xorg,
        yorg,
        dummy_raster,
        -9999.,
        resolution=cellsize,
        srs=srs,
    )
    logger.info("Writing Geotiff clone to {:s}".format(clone_file_tif))
    wt.gdal_writemap(
        clone_file_tif,
        "GTiff",
        xorg,
        yorg,
        dummy_raster,
        -9999.,
        resolution=cellsize,
        zlib=True,
        srs=srs,
    )

    # create grid.xml
    root = ElementTree.Element("regular", locationId=locationid)
    ElementTree.SubElement(root, "rows").text = str(rows)
    ElementTree.SubElement(root, "columns").text = str(cols)
    ElementTree.SubElement(root, "geoDatum").text = geodatum
    ElementTree.SubElement(root, "firstCellCenter")
    ElementTree.SubElement(root[3], "x").text = str(xorg + 0.5 * cellsize)
    ElementTree.SubElement(root[3], "y").text = str(yorg - 0.5 * cellsize)
    ElementTree.SubElement(root, "xCellSize").text = str(cellsize)
    ElementTree.SubElement(root, "yCellSize").text = str(cellsize)
    xml_file = os.path.abspath(os.path.join(destination, "grid.xml"))
    logger.info("Writing Delft-FEWS grid definition to {:s}".format(xml_file))
    gridxml = open(xml_file, "w+")
    gridxml.write(ElementTree.tostring(root))
    gridxml.close()

    # create shape file
    Driver = ogr.GetDriverByName("ESRI Shapefile")
    shp_file = os.path.abspath(os.path.join(destination, "mask.shp"))
    logger.info("Writing shape of clone to {:s}".format(shp_file))
    # for encode see https://gis.stackexchange.com/a/53939
    shp_att = os.path.splitext(os.path.basename(shp_file))[0].encode("utf-8")
    shp = Driver.CreateDataSource(shp_file)
    lyr = shp.CreateLayer(shp_att, srs, geom_type=ogr.wkbPolygon)
    fieldDef = ogr.FieldDefn("ID", ogr.OFTString)
    fieldDef.SetWidth(12)
    lyr.CreateField(fieldDef)
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(xorg, yorg)
    ring.AddPoint(xorg + cols * cellsize, yorg)
    ring.AddPoint(xorg + cols * cellsize, yorg - rows * cellsize)
    ring.AddPoint(xorg, yorg - rows * cellsize)
    ring.AddPoint(xorg, yorg)
    ring.CloseRings
    polygon = ogr.Geometry(ogr.wkbPolygon)
    polygon.AddGeometry(ring)
    feat_out = ogr.Feature(lyr.GetLayerDefn())
    feat_out.SetGeometry(polygon)
    feat_out.SetField("ID", "wflow_mask")
    lyr.CreateFeature(feat_out)
    shp.Destroy()
    logger.info("Model contains {:d} cells".format(cells))
    if cells > 5000000:
        logger.warning(
            "With this amount of cells your model will run VERY slow.\nConsider a larger cell-size.\nFast models run with < 1,000,000 cells"
        )
    elif cells > 1000000:
        logger.warning(
            "With this amount of cells your model will run slow.\nConsider a larger cell-size. Fast models run with < 1,000,000 cells"
        )
    logger, ch = wt.closeLogger(logger, ch)
    del logger, ch
Ejemplo n.º 3
0
def test_ogr2ogr_py_20():
    script_path = test_py_scripts.get_py_script('ogr2ogr')
    if script_path is None:
        pytest.skip()

    try:
        os.remove('tmp/Fields.dbf')
    except OSError:
        pass

    expected_fields = ['a',
                       'A_1',
                       'a_1_2',
                       'aaaaaAAAAA',
                       'aAaaaAAA_1',
                       'aaaaaAAAAB',
                       'aaaaaAAA_2',
                       'aaaaaAAA_3',
                       'aaaaaAAA_4',
                       'aaaaaAAA_5',
                       'aaaaaAAA_6',
                       'aaaaaAAA_7',
                       'aaaaaAAA_8',
                       'aaaaaAAA_9',
                       'aaaaaAAA10']
    expected_data = ['1',
                     '2',
                     '3',
                     '4',
                     '5',
                     '6',
                     '7',
                     '8',
                     '9',
                     '10',
                     '11',
                     '12',
                     '13',
                     '14',
                     '15']

    test_py_scripts.run_py_script(script_path, 'ogr2ogr', 'tmp ../utilities/data/Fields.csv')

    ds = ogr.Open('tmp/Fields.dbf')

    assert ds is not None
    layer_defn = ds.GetLayer(0).GetLayerDefn()
    if layer_defn.GetFieldCount() != 15:
        ds.Destroy()
        ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/Fields.dbf')
        pytest.fail('Unexpected field count: ' + str(ds.GetLayer(0).GetLayerDefn().GetFieldCount()))

    error_occurred = False
    feat = ds.GetLayer(0).GetNextFeature()
    for i in range(layer_defn.GetFieldCount()):
        if layer_defn.GetFieldDefn(i).GetNameRef() != expected_fields[i]:
            print('Expected ', expected_fields[i], ',but got', layer_defn.GetFieldDefn(i).GetNameRef())
            error_occurred = True
        if feat.GetFieldAsString(i) != expected_data[i]:
            print('Expected the value ', expected_data[i], ',but got', feat.GetFieldAsString(i))
            error_occurred = True

    ds.Destroy()
    ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/Fields.dbf')

    assert not error_occurred
Ejemplo n.º 4
0
import osr
import pyproj
import os
import shutil
import argparse

MAX_LAZINESS_DISTANCE = 50000  # 50 Km
HIKER_FRACTION = 0.01  # 1%

jtsk = osr.SpatialReference()
jtsk.ImportFromEPSG(5514)
wgs84 = osr.SpatialReference()
wgs84.ImportFromEPSG(4326)
wgs84_to_jtsk = osr.CoordinateTransformation(wgs84, jtsk)
geod = pyproj.Geod(ellps='WGS84')
shp_driver = ogr.GetDriverByName('ESRI Shapefile')


def load_natural_data(natural_layer):
    forest_data = []
    for i in range(natural_layer.GetFeatureCount()):
        natural = natural_layer.GetNextFeature()
        land_type = natural.GetField('type')
        if land_type == 'forest':
            forest_centroid = natural.GetGeometryRef().Centroid()
            forest_centroid.Transform(wgs84_to_jtsk)
            forest_data.append({
                'centroid': forest_centroid,
                'name': natural.GetField('name'),
            })
    return forest_data
Ejemplo n.º 5
0
    def rasterize(self,
                  buffer_radius=None,
                  raster_template=None,
                  pixel_size=1.99976,
                  value_field='hab_num',
                  float_values=False,
                  array_only=False,
                  out_file_path=None):
        """
        Return a raster that can be used for classification training.

        buffer_radius: A float value in projection units to buffer the
        geometries by. If buffer_radius is left None then only pixels right
        under points will be classified.

        raster_template: A RasterDS object. If supplied, the resulting
        rasterized image will have the same extent and geotransform as the
        template. Also, if a raster_template is provided, the pixel_size keyword
        value will be ignored and pixel size will come from the template.

        pixel_size: A float value representing pixel size in projection units.
        This value will be ignored if a raster_template is supplied.

        value_field: A string representing the name of the field in the
        shapefile that holds the numeric code that will be burned into the
        raster output as the pixel value.

        float_values: Boolean. If `True`, the output raster will contain floats.
        If `False`, the output will be integers. Default is `False`.

        array_only: A boolean. If true we'll try to just write the raster to
        memory and not to disk. If you don't need to keep the raster, this will
        just keep you from having to clean up useless files later. Then we'll
        just return an array instead of GroundTruthRaster object.

        out_file_path: String. Path to the raster file output. If `None`
        (default) and `array_only=False`, a file name based on the
        `GroundTruthShapefile` file name will be created. If `array_only=True`,
        `out_file_path` is ignored.
        """
        if float_values:
            datatype = gdal.GDT_Float32
        else:
            datatype = gdal.GDT_Byte
        # Make a copy of the layer's data source because we'll need to
        # modify its attributes table
        if buffer_radius:
            source_ds = ogr.GetDriverByName("Memory").CopyDataSource(
                self.buffer(radius=buffer_radius), "")
        else:
            source_ds = ogr.GetDriverByName("Memory").CopyDataSource(
                self.ds, "")
        source_layer = source_ds.GetLayer(0)
        source_srs = source_layer.GetSpatialRef()

        if raster_template:
            gTrans = raster_template.gdal_ds.GetGeoTransform()
            pixsizeX = gTrans[1]
            pixsizeY = gTrans[5]
            x_res = raster_template.gdal_ds.RasterXSize
            y_res = raster_template.gdal_ds.RasterYSize
            rdsarr = raster_template.band_array
            # if np.ma.is_masked(rdsarr):
            #     mask = rdsarr[...,0].mask
            # else:
            #     mask = None
        else:
            x_min, x_max, y_min, y_max = source_layer.GetExtent()
            # Create the destination data source
            x_res = int((x_max - x_min) / pixel_size)
            y_res = int((y_max - y_min) / pixel_size)

        if out_file_path:
            targ_fn = out_file_path
        else:
            # make a target ds with filename based on source filename
            targ_fn = self.file_path.rsplit(
                os.path.extsep, 1)[0] + '_rast' + os.path.extsep + 'tif'
        # print "x_res: %i, y_res: %i" % (x_res,y_res)
        target_ds = gdal.GetDriverByName('GTiff').Create(
            targ_fn, x_res, y_res, 1, datatype)

        if raster_template:
            # Use the raster template supplied so that we get the same extent as the raster
            # we're trying to classify
            target_ds.SetGeoTransform(gTrans)
        else:
            # None supplied so use the pixel_size value and the extent of the shapefile
            target_ds.SetGeoTransform((
                x_min,
                pixel_size,
                0,
                y_max,
                0,
                -pixel_size,
            ))
        if raster_template:
            target_ds.SetProjection(raster_template.gdal_ds.GetProjection())
        elif source_srs:
            # Make the target raster have the same projection as the source
            target_ds.SetProjection(source_srs.ExportToWkt())
        else:
            # Source has no projection (needs GDAL >= 1.7.0 to work)
            target_ds.SetProjection('LOCAL_CS["arbitrary"]')
        # Rasterize
        err = gdal.RasterizeLayer(target_ds, [1],
                                  source_layer,
                                  burn_values=[0],
                                  options=["ATTRIBUTE=%s" % value_field])
        if err != 0:
            raise Exception("error rasterizing layer: %s" % err)
        # clean up
        source_layer = None
        source_srs = None
        source_ds = None

        if array_only:
            out_array = target_ds.ReadAsArray()
            target_ds = None
            os.remove(targ_fn)
            return out_array
        else:
            target_ds = None
            return RasterDS(targ_fn)
Ejemplo n.º 6
0
def export_spatial(gdf,
                   path,
                   driver=None,
                   column_names=None,
                   selection=False,
                   virtual=True,
                   chunksize=1000000,
                   **kwargs):
    """ Writes a GeoDataFrame into a spatial file.
    Parameters:
        gdf (object): A GeoVaex DataFrame.
        path (string): The full path of the output file.
        driver (string): The driver to be used to convert the DataFrame into a spatial file.
        column_names (list): List of column names to export or None for all columns.
        selection (bool): Export selection or not
        virtual (bool): When True, export virtual columns.
    """
    geometric_types = [
        ogr.wkbPoint, ogr.wkbLineString, ogr.wkbLinearRing, ogr.wkbPolygon,
        ogr.wkbMultiPoint, ogr.wkbMultiLineString, ogr.wkbMultiPolygon,
        ogr.wkbGeometryCollection
    ]
    field_types = {
        'int': ogr.OFTInteger64,
        'str': ogr.OFTString,
        'flo': ogr.OFTReal
    }
    driver = ogr.GetDriverByName(
        driver) if driver is not None else ogr.GetDriverByName(
            gdf.metadata['driver'])
    if driver is None:
        raise Exception('ERROR: Driver not supported.')
    ds = driver.CreateDataSource(path)
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(gdf.geometry.crs.to_epsg())
    if len(gdf) > 1000:
        sample = gdf.sample(n=1000)
    else:
        sample = gdf
    types = np.unique(pg.get_type_id(sample.geometry))
    if len(types) == 2:
        if 0 in types and 4 in types:
            types = [4]
        elif 1 in types and 5 in types:
            types = [5]
        elif 3 in types and 6 in types:
            types = [6]
    if (len(types) != 1):
        raise Exception('ERROR: Could not write multiple geometries to %s.' %
                        (driver))
    try:
        layer = ds.CreateLayer(gdf.metadata['layer'], srs,
                               geometric_types[types[0]])
    except KeyError:
        layer = ds.CreateLayer((os.path.splitext(path)[1]).split('.')[0], srs,
                               geometric_types[types[0]])
    if layer is None:
        raise Exception(
            'ERROR: Cannot write layer, file extension not consistent with driver, or geometric type incompatible with driver.'
        )
    fields = _get_datatypes(gdf, column_names=column_names, virtual=virtual)
    for field_name in fields:
        key = 'str' if fields[field_name] == 'object' else fields[field_name][
            0:3]
        field = ogr.FieldDefn(field_name, field_types[key])
        if key == 'str':
            field.SetWidth(254)
        elif key == 'flo':
            field.SetWidth(254)
            field.SetPrecision(10)
        layer.CreateField(field)

    geom_arr = gdf.geometry._geometry
    if selection not in [None, False] or gdf.filtered:
        mask = gdf.evaluate_selection_mask(selection)
        geom_arr = geom_arr.filter(mask)

    for i1, i2, chunk in gdf.evaluate_iterator(list(fields.keys()),
                                               selection=selection,
                                               chunk_size=chunksize):
        geom_chunk = geom_arr[i1:i2]
        for i in range(len(chunk[0])):
            feature = ogr.Feature(layer.GetLayerDefn())
            for field_i, field in enumerate(fields):
                value = chunk[field_i][i]
                try:
                    converted_value = value.item()
                except AttributeError:
                    converted_value = value
                feature.SetField(field, converted_value)
            geometry = geom_chunk[i]
            geometry = ogr.CreateGeometryFromWkb(geometry.as_py(
            ) if isinstance(geometry, pa.lib.BinaryScalar) else geometry)
            feature.SetGeometry(geometry)
            layer.CreateFeature(feature)
            feature = None
    ds = None
Ejemplo n.º 7
0
    def open_datasource(self, ogrpath, prefer_mem_copy=True):
        full_ogrpath = None

        # database source ?
        if ogrpath.startswith('PG:'):
            self.is_database_source = True
            full_ogrpath = ogrpath
        else:
            # unsupported access method ?
            ogr_unsupported = ["vsimem", "vsistdout"]
            has_unsup = [m for m in ogr_unsupported if m in ogrpath.split('/')]
            if has_unsup:
                logging.error("Unsupported OGR access method(s) found: %s." %
                              ', '.join(has_unsup))

            # using ogr access methods ?
            ogr_accessmethods = [
                "vsicurl", "vsicurl_streaming", "vsisubfile", "vsistdin"
            ]
            if any([m in ogrpath.split('/') for m in ogr_accessmethods]):
                full_ogrpath = ogrpath
            else:
                filename = ogrpath

                # filter out file access method if present
                ogr_filemethods = [
                    "/vsisparse/", "/vsigzip/", "/vsitar/", "/vsizip/"
                ]
                for fm in ogr_filemethods:
                    if ogrpath.find(fm) == 0:
                        filename = ogrpath[len(fm):]
                        break

                if not os.path.exists(filename):
                    logging.error("The file '%s' does not exist." % filename)
                elif ogrpath == filename:
                    if filename.endswith('.tar') or \
                       filename.endswith('.tgz') or \
                       filename.endswith('.tar.gz'):
                        full_ogrpath = '/vsitar/' + filename
                    elif filename.endswith('.gz'):
                        full_ogrpath = '/vsigzip/' + filename
                    elif filename.endswith('.zip'):
                        full_ogrpath = '/vsizip/' + filename
                    else:
                        full_ogrpath = filename
                else:
                    full_ogrpath = filename

        if full_ogrpath:
            file_datasource = None
            if not self.is_database_source and prefer_mem_copy:
                file_datasource = ogr.Open(full_ogrpath, gdalconst.GA_ReadOnly)
            else:
                self.datasource = ogr.Open(full_ogrpath, gdalconst.GA_ReadOnly)

            if self.is_database_source and not self.datasource:
                logging.error("OGR failed to open connection to %s." %
                              full_ogrpath)
            elif not self.is_database_source and not self.datasource and not file_datasource:
                logging.error(
                    "OGR failed to open '%s', format may be unsupported." %
                    full_ogrpath)
            elif not self.is_database_source and file_datasource and prefer_mem_copy:
                mem_driver = ogr.GetDriverByName('Memory')
                self.datasource = mem_driver.CopyDataSource(
                    file_datasource, 'memoryCopy')
Ejemplo n.º 8
0
def shp_to_rst(shp,
               inSource,
               cellsize,
               nodata,
               outRaster,
               epsg=None,
               rst_template=None,
               snapRst=None,
               api='gdal'):
    """
    Feature Class to Raster
    
    cellsize will be ignored if rst_template is defined
    
    * API's Available:
    - gdal;
    - pygrass;
    - grass;
    """

    if api == 'gdal':
        from osgeo import gdal, ogr
        from gasp.gt.prop.ff import drv_name

        if not epsg:
            from gasp.gt.prop.prj import get_shp_sref
            srs = get_shp_sref(shp).ExportToWkt()
        else:
            from gasp.gt.prop.prj import epsg_to_wkt
            srs = epsg_to_wkt(epsg)

        # Get Extent
        dtShp = ogr.GetDriverByName(drv_name(shp)).Open(shp, 0)

        lyr = dtShp.GetLayer()

        if not rst_template:
            if not snapRst:
                x_min, x_max, y_min, y_max = lyr.GetExtent()
                x_res = int((x_max - x_min) / cellsize)
                y_res = int((y_max - y_min) / cellsize)

            else:
                from gasp.gt.prop.rst import adjust_ext_to_snap

                x_min, y_max, y_res, x_res, cellsize = adjust_ext_to_snap(
                    shp, snapRst)

        else:
            from gasp.gt.fmrst import rst_to_array

            img_temp = gdal.Open(rst_template)
            geo_transform = img_temp.GetGeoTransform()

            y_res, x_res = rst_to_array(rst_template).shape

        # Create output
        dtRst = gdal.GetDriverByName(drv_name(outRaster)).Create(
            outRaster, x_res, y_res, gdal.GDT_Byte)

        if not rst_template:
            dtRst.SetGeoTransform((x_min, cellsize, 0, y_max, 0, -cellsize))

        else:
            dtRst.SetGeoTransform(geo_transform)

        dtRst.SetProjection(str(srs))

        bnd = dtRst.GetRasterBand(1)
        bnd.SetNoDataValue(nodata)

        gdal.RasterizeLayer(dtRst, [1], lyr, burn_values=[1])

        del lyr
        dtShp.Destroy()

    elif api == 'grass' or api == 'pygrass':
        """
        Vectorial geometry to raster
    
        If source is None, the convertion will be based on the cat field.
    
        If source is a string, the convertion will be based on the field
        with a name equal to the given string.
    
        If source is a numeric value, all cells of the output raster will have
        that same value.
        """

        __USE = "cat" if not inSource else "attr" if type(inSource) == str \
            else "val" if type(inSource) == int or \
            type(inSource) == float else None

        if not __USE:
            raise ValueError('\'source\' parameter value is not valid')

        if api == 'pygrass':
            from grass.pygrass.modules import Module

            m = Module("v.to.rast",
                       input=shp,
                       output=outRaster,
                       use=__USE,
                       attribute_column=inSource if __USE == "attr" else None,
                       value=inSource if __USE == "val" else None,
                       overwrite=True,
                       run_=False,
                       quiet=True)

            m()

        else:
            from gasp import exec_cmd

            rcmd = exec_cmd((
                "v.to.rast input={} output={} use={}{} "
                "--overwrite --quiet"
            ).format(
                shp, outRaster, __USE,
                "" if __USE == "cat" else " attribute_column={}".format(inSource) \
                    if __USE == "attr" else " val={}".format(inSource)
            ))

    else:
        raise ValueError('API {} is not available'.format(api))

    return outRaster
Ejemplo n.º 9
0
def _open_layer(driver_name, parameters, dirpath):
    """ Open a layer, return it and its datasource.
    
        Dirpath comes from configuration, and is used to locate files.
    """
    #
    # Set up the driver
    #
    okay_drivers = {
        'postgis': 'PostgreSQL',
        'esri shapefile': 'ESRI Shapefile',
        'postgresql': 'PostgreSQL',
        'shapefile': 'ESRI Shapefile',
        'geojson': 'GeoJSON',
        'spatialite': 'SQLite',
        'oracle': 'OCI',
        'mysql': 'MySQL'
    }

    if driver_name.lower() not in okay_drivers:
        raise KnownUnknown(
            'Got a driver type Vector doesn\'t understand: "%s". Need one of %s.'
            % (driver_name, ', '.join(okay_drivers.keys())))

    driver_name = okay_drivers[driver_name.lower()]
    driver = ogr.GetDriverByName(str(driver_name))

    #
    # Set up the datasource
    #
    if driver_name == 'PostgreSQL':
        if 'dbname' not in parameters:
            raise KnownUnknown(
                'Need at least a "dbname" parameter for postgis')

        conn_parts = []

        for part in ('dbname', 'user', 'host', 'password', 'port'):
            if part in parameters:
                conn_parts.append("%s='%s'" % (part, parameters[part]))

        source_name = 'PG:' + ' '.join(conn_parts)

    elif driver_name == 'MySQL':
        if 'dbname' not in parameters:
            raise KnownUnknown('Need a "dbname" parameter for MySQL')
        if 'table' not in parameters:
            raise KnownUnknown('Need a "table" parameter for MySQL')

        conn_parts = []

        for part in ('host', 'port', 'user', 'password'):
            if part in parameters:
                conn_parts.append("%s=%s" % (part, parameters[part]))

        source_name = 'MySql:' + parameters["dbname"] + "," + ','.join(
            conn_parts) + ",tables=" + parameters['table']

    elif driver_name == 'OCI':
        if 'host' not in parameters:
            raise KnownUnknown('Need a "host" parameter for oracle')
        if 'table' not in parameters:
            raise KnownUnknown('Need a "table" parameter for oracle')
        source_name = 'OCI:'
        source_name = _append_with_delim(source_name, '', parameters, 'user')
        source_name = _append_with_delim(source_name, '/', parameters,
                                         'password')
        if 'user' in parameters:
            source_name = source_name + '@'
        source_name = source_name + parameters['host']
        source_name = _append_with_delim(source_name, ':', parameters, 'port')
        source_name = _append_with_delim(source_name, '/', parameters,
                                         'dbname')
        source_name = source_name + ":" + parameters['table']

    elif driver_name in ('ESRI Shapefile', 'GeoJSON', 'SQLite'):
        if 'file' not in parameters:
            raise KnownUnknown(
                'Need at least a "file" parameter for a shapefile')

        file_href = urljoin(dirpath, parameters['file'])
        scheme, h, file_path, q, p, f = urlparse(file_href)

        if scheme not in ('file', ''):
            raise KnownUnknown('Shapefiles need to be local, not %s' %
                               file_href)

        source_name = file_path

    datasource = driver.Open(str(source_name))

    if datasource is None:
        raise KnownUnknown('Couldn\'t open datasource %s' % source_name)

    #
    # Set up the layer
    #
    if driver_name == 'PostgreSQL' or driver_name == 'OCI' or driver_name == 'MySQL':
        if 'query' in parameters:
            layer = datasource.ExecuteSQL(str(parameters['query']))
        elif 'table' in parameters:
            layer = datasource.GetLayerByName(str(parameters['table']))
        else:
            raise KnownUnknown(
                'Need at least a "query" or "table" parameter for postgis or oracle'
            )
    elif driver_name == 'SQLite':
        layer = datasource.GetLayerByName(str(parameters['layer']))
    else:
        layer = datasource.GetLayer(0)

    if layer.GetSpatialRef() is None and driver_name != 'SQLite':
        raise KnownUnknown('The layer has no spatial reference: %s' %
                           source_name)

    #
    # Return the layer and the datasource.
    #
    # Technically, the datasource is no longer needed
    # but layer segfaults when it falls out of scope.
    #
    return layer, datasource
Ejemplo n.º 10
0
from utils import \
    IngestionError, \
    Bbox

from ie_xml_parser import extract_footprintpolys

is_ie_c_check = False
shp_driver = None
mem_driver = None

try:
    import osgeo.gdal as gdal
    import osgeo.ogr as ogr
    import osgeo.osr as osr
    shp_driver = ogr.GetDriverByName('ESRI Shapefile')
    mem_driver = ogr.GetDriverByName('Memory')
    is_ie_c_check = True
except Exception as e:
    logger.error(
        "ERROR: cannot import/initialise osgeo/ogr; coastline check will fail")
    is_ie_c_check = False


#--------------------------------------------------------------------
# Create an ogr polygon from the data in the Coverage Desr.
# In the cov. descr, the order of coordinate points is N, E
# The returned OGR geometry points have the order x,y (E,N).
#
def extract_geom(coverageDescription, cid, wcs_type):
Ejemplo n.º 11
0
def Write_Dict_To_Shapefile_osgeo(totalList, shapefileName, EPSG):
    '''
    Adapted from
    https://github.com/GeoscienceAustralia/LidarProcessingScripts/RasterIndexTool_GDAL.py
    This function takes a list of dictionaries where each row in the list
    is a feature consisting of the X/Y geometries in the dictionary for each
    item in the list.

    Arguments:
    totalList     -- List of dictionaries
    shapefileName -- Name of the shapefile to be created/overwritten


    '''
    gdal.UseExceptions()

    shapePath = os.path.join(workspace, shapefileName)

    # Get driver
    driver = ogr.GetDriverByName('ESRI Shapefile')

    # Create shapeData, overwrite the data if it exists
    os.chdir(workspace)
    if os.path.exists(shapefileName):
        print 'Shapefile exists and will be deleted'
        driver.DeleteDataSource(shapePath)
        assert not os.path.exists(shapePath)

    shapeData = driver.CreateDataSource(shapefileName)

    # Create spatialReference for output
    outputspatialRef = osr.SpatialReference()

    # Set coordinate reference system to GDA94/MGA zone 56
    outputspatialRef.ImportFromEPSG(EPSG)

    # Create layer
    layer = shapeData.CreateLayer(shapePath,
                                  srs=outputspatialRef,
                                  geom_type=ogr.wkbPolygon)

    # add fields
    fieldNames = ["Date", "Time"]
    for n in range(0, len(fieldNames)):

        # add short text fields - convoluted method but was more extensive
        # in Jonah's script to capture more fields and various field types.
        if fieldNames[n] in ["Date", "Time"]:
            fieldstring = str(fieldNames[n])
            field_name = ogr.FieldDefn(fieldstring, ogr.OFTString)
            field_name.SetWidth(24)
            layer.CreateField(field_name)

    # Create polyline object
    ring = ogr.Geometry(ogr.wkbLinearRing)
    count = 0
    for entry in totalList:
        logAndprint('Row {0} being processed'.format(count))
        dictCounter = 0
        #        while dictCounter < 10:
        logAndprint('Dictionary count {0}'.format(len(totalList[count])))
        for key, value in totalList[count].iteritems():
            logAndprint('\n{0} row, {1} vertex being created'.format(
                count, dictCounter))
            logAndprint('\tKey: {0}, value: {1}'.format(key, value))
            #        print count
            if dictCounter < 2:
                logAndprint('\tRow passed')
                pass
                dictCounter += 1

            else:
                ring.AddPoint(float(key), float(value))
                logAndprint('\t\t{0} and {1} vertex added to ring'.format(
                    key, value))
                dictCounter += 1

        poly = ogr.Geometry(ogr.wkbPolygon)
        logAndprint('Adding geometry')
        poly.AddGeometry(ring)

        # Create feature
        layerDefinition = layer.GetLayerDefn()
        feature = ogr.Feature(layerDefinition)
        feature.SetGeometry(poly)
        # Set the FID field to the count
        feature.SetFID(count)

        # Calculate fields
        logAndprint('Calculating "Date" & "Time" fields')

        # Date is supplied in YYYY/MM/DD
        date = str(totalList[count].keys()[1].split()[0])
        # For animation in ArcGIS the date needs to be in the form
        # DD/MM/YYYY
        reformattedDate = (date.split('/')[2] + '/' + date.split('/')[1] +
                           '/' + date.split('/')[0])
        logAndprint('reformatted date:' + reformattedDate)
        feature.SetField('Date', str(reformattedDate))
        feature.SetField('Time', str(totalList[count].keys()[1].split()[1]))

        # Save feature
        layer.CreateFeature(feature)

        logAndprint('{0} rows processed'.format(count))
        count += 1

        # Empty the ring otherwise the vertices are accumulatied
        ring.Empty()

    # Cleanup
    poly.Destroy()
    feature.Destroy()

    # Cleanup
    shapeData.Destroy()
    # Return
    return shapePath
Ejemplo n.º 12
0
def ogr_odbc_1():

    ogrtest.odbc_drv = None
    if sys.platform != 'win32':
        return 'skip'

    ogrtest.odbc_drv = ogr.GetDriverByName('ODBC')
    if ogrtest.odbc_drv is None:
        return 'skip'

    ds = ogrtest.odbc_drv.Open('data/empty.mdb')
    if ds is None:
        ogrtest.odbc_drv = None
        return 'skip'

    ds = None

    shutil.copy('data/empty.mdb', 'tmp/odbc.mdb')

    # Create and fill tables
    ds = ogrtest.odbc_drv.Open('tmp/odbc.mdb')
    ds.ExecuteSQL(
        "CREATE TABLE test (intfield INT, doublefield DOUBLE, stringfield VARCHAR)"
    )
    ds.ExecuteSQL(
        "INSERT INTO test (intfield, doublefield, stringfield) VALUES (1, 2.34, 'foo')"
    )

    ds.ExecuteSQL(
        "CREATE TABLE test_with_pk (OGR_FID INT PRIMARY KEY, intfield INT, doublefield DOUBLE, stringfield VARCHAR)"
    )
    ds.ExecuteSQL("INSERT INTO test_with_pk (OGR_FID, intfield) VALUES (1, 2)")
    ds.ExecuteSQL("INSERT INTO test_with_pk (OGR_FID, intfield) VALUES (2, 3)")
    ds.ExecuteSQL("INSERT INTO test_with_pk (OGR_FID, intfield) VALUES (3, 4)")
    ds.ExecuteSQL("INSERT INTO test_with_pk (OGR_FID, intfield) VALUES (4, 5)")
    ds.ExecuteSQL("INSERT INTO test_with_pk (OGR_FID, intfield) VALUES (5, 6)")
    ds = None

    # Test with ODBC:user/pwd@dsn syntax
    ds = ogrtest.odbc_drv.Open(
        'ODBC:user/pwd@DRIVER=Microsoft Access Driver (*.mdb);DBQ=tmp/odbc.mdb'
    )
    if ds is None:
        gdaltest.post_reason('failure')
        return 'fail'
    ds = None

    # Test with ODBC:dsn syntax
    ds = ogrtest.odbc_drv.Open(
        'ODBC:DRIVER=Microsoft Access Driver (*.mdb);DBQ=tmp/odbc.mdb')
    if ds is None:
        gdaltest.post_reason('failure')
        return 'fail'
    ds = None

    # Test with ODBC:dsn,table_list syntax
    ds = ogrtest.odbc_drv.Open(
        'ODBC:DRIVER=Microsoft Access Driver (*.mdb);DBQ=tmp/odbc.mdb,test')
    if ds is None:
        gdaltest.post_reason('failure')
        return 'fail'
    if ds.GetLayerCount() != 1:
        gdaltest.post_reason('failure')
        return 'fail'
    ds = None

    # Reopen and check
    ds = ogrtest.odbc_drv.Open('tmp/odbc.mdb')
    if ds.GetLayerCount() != 2:
        gdaltest.post_reason('failure')
        return 'fail'

    lyr = ds.GetLayerByName('test')
    feat = lyr.GetNextFeature()
    if feat.GetField('intfield') != 1 or feat.GetField(
            'doublefield') != 2.34 or feat.GetField('stringfield') != 'foo':
        gdaltest.post_reason('failure')
        feat.DumpReadable()
        return 'fail'

    lyr = ds.GetLayerByName('test_with_pk')
    # Test GetFeatureCount()
    if lyr.GetFeatureCount() != 5:
        gdaltest.post_reason('failure')
        return 'fail'

    # Test GetFeature()
    feat = lyr.GetFeature(4)
    if feat.GetField('intfield') != 5:
        gdaltest.post_reason('failure')
        feat.DumpReadable()
        return 'fail'

    # Test SetAttributeFilter()
    lyr.SetAttributeFilter('intfield = 6')
    feat = lyr.GetNextFeature()
    if feat.GetFID() != 5:
        gdaltest.post_reason('failure')
        feat.DumpReadable()
        return 'fail'

    # Test ExecuteSQL()
    sql_lyr = ds.ExecuteSQL("SELECT * FROM test")
    feat = sql_lyr.GetNextFeature()
    if feat.GetField('intfield') != 1 or feat.GetField(
            'doublefield') != 2.34 or feat.GetField('stringfield') != 'foo':
        gdaltest.post_reason('failure')
        feat.DumpReadable()
        return 'fail'
    ds.ReleaseResultSet(sql_lyr)

    ds = None

    return 'success'
    def TranslateToReducedGeoJSON(self,FileName):
        """This converts the point data to a GeoJSON

        Args:
            FileName (str): the name of the file to be printed. The code strips the extension and turns it into .geojson, so you can give it the name of the csv file and ti will still work.

        Return:
            None, but prints a new GeoJSON

        Author: SMM
        """

        # Parse a delimited text file of volcano data and create a shapefile

        import osgeo.ogr as ogr
        import osgeo.osr as osr


        #  set up the shapefile driver
        driver = ogr.GetDriverByName("GeoJSON")

        # Get the path to the file
        this_path = LSDOst.GetPath(FileName)
        DataName = self.FilePrefix

        FileOut = this_path+DataName+".geojson"

        print("The filename will be: " + FileOut)

        # delete the existing file
        if os.path.exists(FileOut):
            driver.DeleteDataSource(FileOut)

        # create the data source
        data_source = driver.CreateDataSource(FileOut)

        # create the spatial reference,  in this case WGS84 (which is ESPG 4326)
        srs = osr.SpatialReference()
        srs.ImportFromEPSG(4326)

        print("Creating the layer")

        # create the layer
        layer = data_source.CreateLayer("PointData", srs, ogr.wkbPoint)

        #print "Adding the field names"

        # Add the field names
        for index,name in enumerate(self.VariableList):
            print("The variable name is " + name + " and the type is: " + str(self.DataTypes[index]))


            if self.DataTypes[index] is int:
                layer.CreateField(ogr.FieldDefn(name, ogr.OFTInteger))
            elif self.DataTypes[index] is float:
                layer.CreateField(ogr.FieldDefn(name, ogr.OFTReal))
            elif self.DataTypes[index] is str:
                print("Making a sting layer for layer " + name)
                layer.CreateField(ogr.FieldDefn(name, ogr.OFTString))
            else:
                layer.CreateField(ogr.FieldDefn(name, ogr.OFTReal))

        # Process the text file and add the attributes and features to the shapefile
        for index,lat in enumerate(self.Latitude):

            # create the feature
            feature = ogr.Feature(layer.GetLayerDefn())

            for name in self.VariableList:
                feature.SetField(name, self.PointData[name][index])

            # create the WKT for the feature using Python string formatting
            wkt = "POINT(%f %f)" %  (float(self.Longitude[index]), float(self.Latitude[index]))

            # Create the point from the Well Known Txt
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            # Create the feature in the layer (shapefile)
            layer.CreateFeature(feature)
            # Destroy the feature to free resources
            feature.Destroy()

        # Destroy the data source to free resources
        data_source.Destroy()
Ejemplo n.º 14
0
    sys.exit(1)

gcp_srs = ds.GetGCPProjection()
gcps = ds.GetGCPs()

ds = None

if gcps is None or len(gcps) == 0:
    print('No GCPs on file %s!' % in_file)
    sys.exit(1)

# ----------------------------------------------------------------------------
# Create output file, and layer.
# ----------------------------------------------------------------------------

drv = ogr.GetDriverByName( out_format )
if drv is None:
    print('No driver named %s available.' % out_format)
    sys.exit(1)

ds = drv.CreateDataSource( out_file )

if pixel_out == 0 and gcp_srs != "":
    srs = osr.SpatialReference()
    srs.ImportFromWkt(gcp_srs)
else:
    srs = None

if pixel_out == 0:
    geom_type = ogr.wkbPoint25D
else:
Ejemplo n.º 15
0
    def get_df_only_with_inside_country_points(self,
                                               df,
                                               name_index: str = 'NAME_ISO',
                                               country_index: str = 'COUNTRY',
                                               lat_index: str = 'LATITUDE',
                                               lon_index: str = 'LONGITUDE'):
        """
    Returns a filtered df without the missmarked occurrences, checking if they realy are inside the country

    Parameters
    ----------
    df : Dataframe
        Dataframe with gbif occurenced not checked
    name_index : str
        Column with name information 
    country_index : str
        Column with country information  
    lat_index : str
        Column with latitude information
    lon_index : str
        Column with longitude information
    """

        # 1 --> Open shapefile containing country polygons
        filename = self._brazil_country_level_path
        drv = ogr.GetDriverByName(
            'ESRI Shapefile')  # set up driver object to read/write shapefiles
        shapefile = drv.Open(filename)  # open shapefile
        layer = shapefile.GetLayer(0)  # create layer object for shapefile

        #2 --> Determine indices of relevant columns
        nameIndex = layer.GetLayerDefn().GetFieldIndex(
            name_index)  # index of column with country names in shapefile
        countryIndex = df.columns.get_loc(
            country_index)  # index of country name column in observation table
        latIndex = df.columns.get_loc(
            lat_index)  # index of lat column in observation table
        lonIndex = df.columns.get_loc(
            lon_index)  # index of lon column in observation table

        #3 --> Countries Check.
        geocodingResults = [
        ]  #Set up list in which we will store the results of looking up the containing country for each row

        # itertuples is used to efficiently loop through the rows of the data frame
        for row in df.itertuples(index=False):

            # create a new OGR point object with lon and lat coordinates from current row
            pt = ogr.Geometry(ogr.wkbPoint)
            pt.SetPoint_2D(0, row[lonIndex], row[latIndex])

            # apply spatial filter that will give us polygons that intersect our point
            layer.SetSpatialFilter(pt)

            country = "UNKNOWN"  # variable for storing the country's name

            # check whether there's exactly one feature selected, if get country name of that feature
            if layer.GetFeatureCount() == 1:
                country = layer.GetNextFeature().GetFieldAsString(
                    nameIndex).title()

            # add country name to result list
            geocodingResults.append(country)

        df['country_geocoding'] = geocodingResults  # add geocoding results as new column
        df = df[(df.country_geocoding == df.COUNTRY) |
                (df.country_geocoding == 'Brazil')]

        #4 --> Limits Check.
        df = df[df['LONGITUDE'] >= self.x_min_limit]
        df = df[df['LONGITUDE'] <= self.x_max_limit]
        df = df[df['LATITUDE'] >= self.y_min_limit]
        df = df[df['LATITUDE'] <= self.y_max_limit]
        return df
Ejemplo n.º 16
0
template_file = r'3arcsecond_template.tif'
print(template_file)

template = gdal.Open(template_file)
template_transform = template.GetGeoTransform()
template_w = template.RasterXSize
template_h = template.RasterYSize

target = gdal.GetDriverByName('GTiff').Create('test.tif', template_w,
                                              template_h, 1, gdal.GDT_Byte,
                                              ["COMPRESS=LZW"])

target.SetGeoTransform(template_transform)
target.SetProjection(template.GetProjection())

ds = ogr.GetDriverByName('Memory').CreateDataSource('wrk')
layer = ds.CreateLayer('poly')

for name, geom in cursor:
    print(name)
    # print(geom)
    # print(dir(geom))

    feature = ogr.Feature(layer.GetLayerDefn())
    feature.SetGeometryDirectly(ogr.Geometry(wkt=str(geom)))

    layer.CreateFeature(feature)

gdal.RasterizeLayer(target, [1], layer, None, None, [1], ['ALL_TOUCHED=TRUE'])

#x = target.GetRasterBand(1).ReadAsArray()
Ejemplo n.º 17
0
def gdal_polygonize(src_filename: Optional[str] = None,
                    band_number: Union[int, str] = 1,
                    dst_filename: Optional[str] = None,
                    driver_name: str = 'GTiff',
                    dst_layername: Optional[str] = None,
                    dst_fieldname: Optional[str] = None,
                    quiet: bool = False,
                    mask: str = 'default',
                    options: Optional[list] = None,
                    connectedness8: bool = False):

    if isinstance(band_number, str) and not band_number.startswith('mask'):
        band_number = int(band_number)

    if connectedness8:
        options.append('8CONNECTED=8')

    if driver_name is None:
        driver_name = GetOutputDriverFor(dst_filename, is_raster=False)

    if dst_layername is None:
        dst_layername = 'out'

    options = options or []

    # =============================================================================
    # 	Verify we have next gen bindings with the polygonize method.
    # =============================================================================
    try:
        gdal.Polygonize
    except AttributeError:
        print('')
        print(
            'gdal.Polygonize() not available.  You are likely using "old gen"')
        print('bindings or an older version of the next gen bindings.')
        print('')
        return 1

    # =============================================================================
    # Open source file
    # =============================================================================

    src_ds = gdal.Open(src_filename)

    if src_ds is None:
        print('Unable to open %s' % src_filename)
        return 1

    if band_number == 'mask':
        srcband = src_ds.GetRasterBand(1).GetMaskBand()
        # Workaround the fact that most source bands have no dataset attached
        options.append('DATASET_FOR_GEOREF=' + src_filename)
    elif isinstance(band_number, str) and band_number.startswith('mask,'):
        srcband = src_ds.GetRasterBand(int(
            band_number[len('mask,'):])).GetMaskBand()
        # Workaround the fact that most source bands have no dataset attached
        options.append('DATASET_FOR_GEOREF=' + src_filename)
    else:
        srcband = src_ds.GetRasterBand(band_number)

    if mask == 'default':
        maskband = srcband.GetMaskBand()
    elif mask == 'none':
        maskband = None
    else:
        mask_ds = gdal.Open(mask)
        maskband = mask_ds.GetRasterBand(1)

    # =============================================================================
    #       Try opening the destination file as an existing file.
    # =============================================================================

    try:
        gdal.PushErrorHandler('CPLQuietErrorHandler')
        dst_ds = ogr.Open(dst_filename, update=1)
        gdal.PopErrorHandler()
    except:
        dst_ds = None

    # =============================================================================
    # 	Create output file.
    # =============================================================================
    if dst_ds is None:
        drv = ogr.GetDriverByName(driver_name)
        if not quiet:
            print('Creating output %s of format %s.' %
                  (dst_filename, driver_name))
        dst_ds = drv.CreateDataSource(dst_filename)

    # =============================================================================
    #       Find or create destination layer.
    # =============================================================================
    try:
        dst_layer = dst_ds.GetLayerByName(dst_layername)
    except:
        dst_layer = None

    dst_field: int = -1
    if dst_layer is None:

        srs = src_ds.GetSpatialRef()
        dst_layer = dst_ds.CreateLayer(dst_layername,
                                       geom_type=ogr.wkbPolygon,
                                       srs=srs)

        if dst_fieldname is None:
            dst_fieldname = 'DN'

        fd = ogr.FieldDefn(dst_fieldname, ogr.OFTInteger)
        dst_layer.CreateField(fd)
        dst_field = 0
    else:
        if dst_fieldname is not None:
            dst_field = dst_layer.GetLayerDefn().GetFieldIndex(dst_fieldname)
            if dst_field < 0:
                print("Warning: cannot find field '%s' in layer '%s'" %
                      (dst_fieldname, dst_layername))

    # =============================================================================
    # Invoke algorithm.
    # =============================================================================

    if quiet:
        prog_func = None
    else:
        prog_func = gdal.TermProgress_nocb

    result = gdal.Polygonize(srcband,
                             maskband,
                             dst_layer,
                             dst_field,
                             options,
                             callback=prog_func)

    srcband = None
    src_ds = None
    dst_ds = None
    mask_ds = None

    return result
Ejemplo n.º 18
0
    def open(self):
        if self.options.input:
            self.in_ds = ogr.Open(self.options.input)
        else:
            raise Exception("No input layer was specified")
        if self.options.layer:
            self.input = self.in_ds.GetLayerByName(self.options.layer)
            if not self.input:
                raise Exception("The layer '%s' was not found" %
                                self.options.layer)
        else:
            self.input = self.in_ds.GetLayer(0)

        if self.options.a_srs:
            self.in_srs = osr.SpatialReference()
            self.in_srs.SetFromUserInput(self.options.a_srs)
        else:
            self.in_srs = self.input.GetSpatialRef()

        if self.options.spat:
            self.input.SetSpatialFilterRect(*self.options.spat)

        self.out_drv = ogr.GetDriverByName(self.options.driver)

        if self.options.where:
            self.input.SetAttributeFilter(self.options.where)

        if not self.out_drv:
            raise Exception(
                "The '%s' driver was not found, did you misspell it or is it not available in this GDAL build?",
                self.options.driver)
        if not self.out_drv.TestCapability('CreateDataSource'):
            raise Exception(
                "The '%s' driver does not support creating layers, you will have to choose another output driver",
                self.options.driver)
        if not self.options.output:
            raise Exception("No output layer was specified")
        if self.options.driver == 'ESRI Shapefile':
            path, filename = os.path.split(os.path.abspath(
                self.options.output))
            name, ext = os.path.splitext(filename)
            if self.options.overwrite:
                # special case the Shapefile driver, which behaves specially.
                if os.path.exists(os.path.join(
                        path,
                        name,
                ) + '.shp'):
                    os.remove(os.path.join(
                        path,
                        name,
                    ) + '.shp')
                    os.remove(os.path.join(
                        path,
                        name,
                    ) + '.shx')
                    os.remove(os.path.join(
                        path,
                        name,
                    ) + '.dbf')
            else:
                if os.path.exists(os.path.join(
                        path,
                        name,
                ) + ".shp"):
                    raise Exception(
                        "The file '%s' already exists, but the overwrite option is not specified"
                        % (os.path.join(
                            path,
                            name,
                        ) + ".shp"))

        if self.options.overwrite:
            dsco = ('OVERWRITE=YES', )
        else:
            dsco = (),

        self.out_ds = self.out_drv.CreateDataSource(self.options.output, dsco)

        if self.options.t_srs:
            self.out_srs = osr.SpatialReference()
            self.out_srs.SetFromUserInput(self.options.t_srs)
        else:
            self.out_srs = None
        self.output = self.out_ds.CreateLayer(
            self.options.output,
            geom_type=self.input.GetLayerDefn().GetGeomType(),
            srs=self.out_srs)
Ejemplo n.º 19
0
def test_gdal_polygonize_1():

    script_path = test_py_scripts.get_py_script('gdal_polygonize')
    if script_path is None:
        return 'skip'

    # Create a OGR datasource to put results in.
    shp_drv = ogr.GetDriverByName('ESRI Shapefile')
    try:
        os.stat('tmp/poly.shp')
        shp_drv.DeleteDataSource('tmp/poly.shp')
    except:
        pass

    shp_ds = shp_drv.CreateDataSource('tmp/poly.shp')

    shp_layer = shp_ds.CreateLayer('poly', None, ogr.wkbPolygon)

    fd = ogr.FieldDefn('DN', ogr.OFTInteger)
    shp_layer.CreateField(fd)

    shp_ds.Destroy()

    # run the algorithm.
    test_py_scripts.run_py_script(script_path, 'gdal_polygonize',
                                  '../alg/data/polygonize_in.grd tmp poly DN')

    # Confirm we get the set of expected features in the output layer.

    shp_ds = ogr.Open('tmp')
    shp_lyr = shp_ds.GetLayerByName('poly')

    expected_feature_number = 13
    if shp_lyr.GetFeatureCount() != expected_feature_number:
        gdaltest.post_reason(
            'GetFeatureCount() returned %d instead of %d' %
            (shp_lyr.GetFeatureCount(), expected_feature_number))
        return 'fail'

    expect = [107, 123, 115, 115, 140, 148, 123, 140, 156, 100, 101, 102, 103]

    tr = ogrtest.check_features_against_list(shp_lyr, 'DN', expect)

    # check at least one geometry.
    if tr:
        shp_lyr.SetAttributeFilter('dn = 156')
        feat_read = shp_lyr.GetNextFeature()
        if ogrtest.check_feature_geometry(
                feat_read,
                'POLYGON ((440720 3751200,440900 3751200,440900 3751020,440720 3751020,440720 3751200),(440780 3751140,440780 3751080,440840 3751080,440840 3751140,440780 3751140))'
        ) != 0:
            tr = 0
        feat_read.Destroy()

    shp_ds.Destroy()
    # Reload drv because of side effects of run_py_script()
    shp_drv = ogr.GetDriverByName('ESRI Shapefile')
    shp_drv.DeleteDataSource('tmp/poly.shp')

    if tr:
        return 'success'
    else:
        return 'fail'
Ejemplo n.º 20
0
tempDF = siteDF.iloc[selectSite]
finalSite = tempDF[tempDF['peak_end_date'] > datetime(2013, 12, 31)]

for site_no in finalSite.site_no.tolist():
    filePath = 'https://nwis.waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=' + site_no + '&referred_module=sw&period=&begin_date=2003-01-01&end_date=2013-12-31'
    downloadDischarge(filePath)

list_gauges = glob('gauge/*.csv')
list_gauges = [os.path.basename(x).split('_')[0] for x in list_gauges]
finalSite0 = finalSite[finalSite.site_no.isin(list_gauges)]
lats = finalSite0.dec_lat_va.tolist()
lons = finalSite0.dec_long_va.tolist()
siteName = finalSite0.station_nm.tolist()
siteNo = finalSite0.site_no.tolist()
finalSite0.to_csv('final_site.csv', index=False)
driver = ogr.GetDriverByName("ESRI Shapefile")

# create the data source
data_source = driver.CreateDataSource("river_stations.shp")

# create the layer
srs = osr.SpatialReference()
layer = data_source.CreateLayer("river_stations", srs, ogr.wkbPoint)

# Add the fields we're interested in
field_region = ogr.FieldDefn("Station", ogr.OFTString)
field_region.SetWidth(24)
layer.CreateField(field_region)
site_region = ogr.FieldDefn("Site No", ogr.OFTString)
site_region.SetWidth(24)
layer.CreateField(site_region)
Ejemplo n.º 21
0
    def getGeocodes(self, shapeFile, projectionFile, gridFile, gridShape):
        outRef ='GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]'
        inRef2 = 'PROJCS["Sinusoidal",GEOGCS["GCS_Undefined",DATUM["Undefined",SPHEROID["User_Defined_Spheroid",6371007.181,0.0]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Sinusoidal"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],UNIT["Meter",1.0]]'
        
        driver = ogr.GetDriverByName("ESRI Shapefile")
        dataSource = driver.Open(shapeFile, 0)

        if dataSource == None:
            print("> No shx file in the shape zip. Try to restore shx.")
            cmd = ["ogrinfo", shapeFile, "--config", "SHAPE_RESTORE_SHX", "YES"]
            p = sp.Popen(cmd,stdout=sp.PIPE,stderr=sp.STDOUT,cwd=self.WORKING_DIR)
            for line in iter(p.stdout.readline, b''):
                print("> " + line.decode().rstrip())
            dataSource = driver.Open(shapeFile, 0)

        layer = dataSource.GetLayer()
        minX, maxX, minY, maxY = layer.GetExtent()

        # minY, max, minY, maxY = layer.GetExtent()


        # Create ring
        ring = ogr.Geometry(ogr.wkbLinearRing)
        ring.AddPoint(minX, minY), ring.AddPoint(maxX, minY), ring.AddPoint(maxX, maxY), ring.AddPoint(minX, maxY), ring.AddPoint(minX, minY)

        # Create polygon
        mbb = ogr.Geometry(ogr.wkbPolygon)
        mbb.AddGeometry(ring)
        # # Transform
        inSpatialRef = layer.GetSpatialRef()
        inSpatialRef.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        # print inSpatialRef
        outSpatialRef = osr.SpatialReference()
        # outSpatialRef.ImportFromEPSG(4326)
        outSpatialRef.ImportFromWkt(outRef)
        outSpatialRef.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
        mbb.Transform(coordTransform)
        mbb.FlattenTo2D()

        print(mbb.GetEnvelope())

        # check from grid shapefile
        list1 = []
        driver2 = ogr.GetDriverByName("ESRI Shapefile")
        dataSource2 = driver2.Open(gridShape, 0)
        layer2 = dataSource2.GetLayer()
        # inSpatialRef2 = layer2.GetSpatialRef()
        inSpatialRef2 = osr.SpatialReference()
        inSpatialRef2.ImportFromWkt(inRef2)
        inSpatialRef2.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        outSpatialRef2 = osr.SpatialReference()
        outSpatialRef2.ImportFromWkt(outRef)
        outSpatialRef2.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        coordTransform2 = osr.CoordinateTransformation(inSpatialRef2, outSpatialRef2)
        # print((inSpatialRef2, outSpatialRef2))


        for feature in layer2:
            geom = feature.GetGeometryRef()
            geom.Transform(coordTransform2)
            if(geom.Contains(mbb)):
                if geom.IsValid() and geom.IsSimple():
                    print('contains', geom.GetEnvelope())
                    list1.append("h" + str(int(feature.GetField("h"))).zfill(2) + "v" + str(int(feature.GetField("v"))).zfill(2))


        if len(list1) == 0:
            for i in range(layer2.GetFeatureCount()):
                feature = layer2.GetFeature(i)
                geom = feature.GetGeometryRef()
                geom.Transform(coordTransform2)

                if(geom.Intersects(mbb)):
                    if geom.IsValid() and geom.IsSimple():
                        print('intersect', geom.GetEnvelope())
                        list1.append("h" + str(int(feature.GetField("h"))).zfill(2) + "v" + str(int(feature.GetField("v"))).zfill(2))

        # final check with grid.txt
        list2 = []
        f = open(gridFile, 'rU')
        f.readline()
        for line in f:
            l = line.strip()
            items = l.split()

            minx = float(items[2])            
            maxx = float(items[3])
            miny = float(items[4])
            maxy = float(items[5])            

            tempRing = ogr.Geometry(ogr.wkbLinearRing)
            tempRing.AddPoint(minx, miny), tempRing.AddPoint(maxx, miny), tempRing.AddPoint(maxx, maxy), tempRing.AddPoint(minx, maxy), tempRing.AddPoint(minx, miny)

            tempMbb = ogr.Geometry(ogr.wkbPolygon)
            tempMbb.AddGeometry(tempRing)

            ivihPolygon = [items[0], items[1], tempMbb]

            # print(tempMbb.GetEnvelope())

            if(tempMbb.Contains(mbb)):
                list2.append("h" + items[1].zfill(2) + "v" + items[0].zfill(2))
            elif(tempMbb.Intersects(mbb)):
                list2.append("h" + items[1].zfill(2) + "v" + items[0].zfill(2))

        geocodes = list(set(list1).intersection(list2))

        # print(list1, list2)


        if len(geocodes) == 0:
            print("Getting intersect geocode failed")
            raise Exception('')

        print(geocodes, end='')
        print("is (are) extracted!")        

        return geocodes
Ejemplo n.º 22
0
def ogr_dispatch(argv, progress=None, progress_arg=None):

    src_filename = None
    dst_filename = None
    format = "ESRI Shapefile"
    options = Options()
    lco = []
    dsco = []
    pszWHERE = None

    if len(argv) == 0:
        return Usage()

    i = 0
    while i < len(argv):
        arg = argv[i]
        if EQUAL(arg, '-src') and i + 1 < len(argv):
            i = i + 1
            src_filename = argv[i]
        elif EQUAL(arg, '-dst') and i + 1 < len(argv):
            i = i + 1
            dst_filename = argv[i]
        elif EQUAL(arg, '-f') and i + 1 < len(argv):
            i = i + 1
            format = argv[i]

        elif EQUAL(arg, '-a_srs') and i + 1 < len(argv):
            i = i + 1
            pszOutputSRSDef = argv[i]
            if EQUAL(pszOutputSRSDef, "NULL") or \
               EQUAL(pszOutputSRSDef, "NONE"):
                options.bNullifyOutputSRS = True
            else:
                options.poOutputSRS = osr.SpatialReference()
                if options.poOutputSRS.SetFromUserInput(pszOutputSRSDef) != 0:
                    print("Failed to process SRS definition: %s" % pszOutputSRSDef)
                    return 1
        elif EQUAL(arg, '-dsco') and i + 1 < len(argv):
            i = i + 1
            dsco.append(argv[i])
        elif EQUAL(arg, '-lco') and i + 1 < len(argv):
            i = i + 1
            lco.append(argv[i])
        elif EQUAL(arg, '-field') and i + 1 < len(argv):
            i = i + 1
            options.dispatch_fields.append(argv[i])
        elif EQUAL(arg, '-25D_as_2D'):
            options.b25DAs2D = True
        elif EQUAL(arg, '-multi_as_single'):
            options.bMultiAsSingle = True
        elif EQUAL(arg, '-remove_dispatch_fields'):
            options.bRemoveDispatchFields = True
        elif EQUAL(arg, '-prefix_with_layer_name'):
            options.bPrefixWithLayerName = True
        elif EQUAL(arg, '-style_as_field'):
            options.bStyleAsField = True
        elif (EQUAL(arg, "-tg") or
                EQUAL(arg, "-gt")) and i + 1 < len(argv):
            i = i + 1
            options.nGroupTransactions = int(argv[i])
        elif EQUAL(arg, "-where") and i + 1 < len(argv):
            i = i + 1
            pszWHERE = argv[i]
        elif EQUAL(arg, '-quiet'):
            options.bQuiet = True
        else:
            print('Unrecognized argument : %s' % arg)
            return Usage()
        i = i + 1

    if src_filename is None:
        print('Missing -src')
        return 1

    if dst_filename is None:
        print('Missing -dst')
        return 1

    if len(options.dispatch_fields) == 0:
        print('Missing -dispatch_field')
        return 1

    src_ds = ogr.Open(src_filename)
    if src_ds is None:
        print('Cannot open source datasource %s' % src_filename)
        return 1

    dst_ds = ogr.Open(dst_filename, update=1)
    if dst_ds is not None:
        if len(dsco) != 0:
            print('-dsco should not be specified for an existing datasource')
            return 1
    else:
        dst_ds = ogr.GetDriverByName(format).CreateDataSource(dst_filename, options=dsco)
    if dst_ds is None:
        print('Cannot open or create target datasource %s' % dst_filename)
        return 1

    layerMap = {}

    for src_lyr in src_ds:
        if pszWHERE is not None:
            src_lyr.SetAttributeFilter(pszWHERE)
        ret = convert_layer(src_lyr, dst_ds, layerMap, options)
        if ret != 0:
            return ret

    return 0
Ejemplo n.º 23
0
def convert_vfr(ids,
                odsn,
                frmt,
                layers=[],
                overwrite=False,
                options=[],
                geom_name=None,
                mode=Mode.write,
                nogeomskip=True,
                userdata={}):
    odrv = ogr.GetDriverByName(frmt)
    if odrv is None:
        fatal("Format '%s' is not supported" % frmt)

    # try to open datasource
    ods = odrv.Open(odsn, True)
    if ods is None:
        # if fails, try to create new datasource
        ods = odrv.CreateDataSource(odsn)
    if ods is None:
        fatal("Unable to open or create new datasource '%s'" % odsn)

    create_geom = ods.TestCapability(ogr.ODsCCreateGeomFieldAfterCreateLayer)
    if not geom_name and not create_geom:
        warning("Driver '%s' doesn't support multiple geometry columns. "
                "Only first will be used." % odrv.GetName())

    # OVERWRITE is not support by Esri Shapefile
    if overwrite:
        if frmt != 'ESRI Shapefile':
            options.append("OVERWRITE=YES")
        if mode == Mode.write:
            # delete also layers which are not part of ST_UKSH
            for layer in ("ulice", "parcely", "stavebniobjekty",
                          "adresnimista"):
                if ods.GetLayerByName(layer) is not None:
                    ods.DeleteLayer(layer)

    # process features marked for deletion first
    dlist = None  # statistics
    if mode == Mode.change:
        dlayer = ids.GetLayerByName('ZaniklePrvky')
        if dlayer:
            dlist = process_deleted_features(dlayer, ods, layers)

    # process layers
    start = time.time()
    nlayers = ids.GetLayerCount()
    nfeat = 0
    for iLayer in range(nlayers):
        layer = ids.GetLayer(iLayer)
        layer_name = layer.GetName()
        ### force lower case for output layers, some drivers are doing
        ### that automatically anyway
        layer_name_lower = layer_name.lower()

        if layers and layer_name not in layers:
            # process only selected layers
            continue

        if layer_name == 'ZaniklePrvky':
            # skip deleted features (already done)
            continue

        olayer = ods.GetLayerByName('%s' % layer_name_lower)
        sys.stdout.write("Processing layer %-20s ..." % layer_name)
        if not overwrite and (olayer and mode == Mode.write):
            sys.stdout.write(
                " already exists (use --overwrite or --append to modify existing data)\n"
            )
            continue

        ### TODO: fix output drivers not to use default geometry
        ### names
        if frmt in ('PostgreSQL', 'OCI') and not geom_name:
            if layer_name_lower == 'ulice':
                remove_option(options, 'GEOMETRY_NAME')
                options.append('GEOMETRY_NAME=definicnicara')
            else:
                remove_option(options, 'GEOMETRY_NAME')
                options.append('GEOMETRY_NAME=definicnibod')

        # delete layer if exists and append is not True
        if olayer and mode == Mode.write:
            if delete_layer(ids, ods, layer_name_lower):
                olayer = None

        # create new output layer if not exists
        if not olayer:
            olayer = create_layer(ods, layer, layer_name_lower, geom_name,
                                  create_geom, options)
        if olayer is None:
            fatal("Unable to export layer '%s'. Exiting..." % layer_name)

        # pre-process changes
        if mode == Mode.change:
            change_list = process_changes(layer, olayer)
            if dlist and layer_name in dlist:  # add features to be deleted
                change_list.update(dlist[layer_name])

        ifeat = n_nogeom = 0
        geom_idx = -1

        # make sure that PG sequence is up-to-date (import for fid == -1)
        fid = -1
        if 'pgconn' in userdata:
            fid = get_fid_max(userdata['pgconn'], layer_name_lower)
            if fid > 0:
                update_fid_seq(userdata['pgconn'], layer_name_lower, fid)
        if fid is None or fid == -1:
            fid = olayer.GetFeatureCount()

        # start transaction in output layer
        if olayer.TestCapability(ogr.OLCTransactions):
            olayer.StartTransaction()

        # delete marked features first (changes only)
        if mode == Mode.change and dlist and layer_name in dlist:
            for fid in dlist[layer_name].keys():
                olayer.DeleteFeature(fid)

        # do mapping for fields (needed for Esri Shapefile when
        # field names are truncated)
        field_map = [i for i in range(0, layer.GetLayerDefn().GetFieldCount())]

        # copy features from source to destination layer
        layer.ResetReading()
        feature = layer.GetNextFeature()
        while feature:
            # check for changes first (delete/update/add)
            if mode == Mode.change:
                c_fid = feature.GetFID()
                action, o_fid = change_list.get(c_fid, (None, None))
                if action is None:
                    fatal("Layer %s: unable to find feature %d" %
                          (layer_name, c_fid))

                # feature marked to be changed (delete first)
                if action in (Action.delete, Action.update):
                    olayer.DeleteFeature(o_fid)

                # determine fid for new feature
                if action == Action.add:
                    fid = -1
                else:
                    fid = o_fid

                if action == Action.delete:
                    # do nothing and continue
                    feature = layer.GetNextFeature()
                    ifeat += 1
                    continue
            else:
                fid += 1

            # clone feature
            ### ofeature = feature.Clone() # replace by SetFrom()
            ofeature = ogr.Feature(olayer.GetLayerDefn())
            ofeature.SetFromWithMap(feature, True, field_map)

            # modify geometry columns if requested
            if geom_name:
                if geom_idx < 0:
                    geom_idx = feature.GetGeomFieldIndex(geom_name)

                    # delete remaining geometry columns
                    ### not needed - see SetFrom()
                    ### odefn = ofeature.GetDefnRef()
                    ### for i in range(odefn.GetGeomFieldCount()):
                    ###    if i == geom_idx:
                    ###        continue
                    ###    odefn.DeleteGeomFieldDefn(i)

                modify_feature(feature, geom_idx, ofeature)

            if ofeature.GetGeometryRef() is None:
                n_nogeom += 1
                if nogeomskip:
                    # skip feature without geometry
                    feature = layer.GetNextFeature()
                    ofeature.Destroy()
                    continue

            # set feature id
            if fid >= -1:
                # fid == -1 -> unknown fid
                ofeature.SetFID(fid)

            # add new feature to output layer
            olayer.CreateFeature(ofeature)

            feature = layer.GetNextFeature()
            ifeat += 1

        # commit transaction in output layer
        if olayer.TestCapability(ogr.OLCTransactions):
            olayer.CommitTransaction()

        # print statistics per layer
        sys.stdout.write(" %10d features" % ifeat)
        if mode == Mode.change:
            n_added = n_updated = n_deleted = 0
            for action, unused in change_list.itervalues():
                if action == Action.update:
                    n_updated += 1
                elif action == Action.add:
                    n_added += 1
                else:  # Action.delete:
                    n_deleted += 1
            sys.stdout.write(" (%5d added, %5d updated, %5d deleted)" % \
                                 (n_added, n_updated, n_deleted))
        else:
            sys.stdout.write(" added")
            if n_nogeom > 0:
                if nogeomskip:
                    sys.stdout.write(" (%d without geometry skipped)" %
                                     n_nogeom)
                else:
                    sys.stdout.write(" (%d without geometry)" % n_nogeom)
        sys.stdout.write("\n")

        nfeat += ifeat

        # update sequence for PG
        if 'pgconn' in userdata:
            ### fid = get_fid_max(userdata['pgconn'], layer_name_lower)
            if fid > 0:
                update_fid_seq(userdata['pgconn'], layer_name_lower, fid)

    # close output datasource
    ods.Destroy()

    # final statistics (time elapsed)
    message("Time elapsed: %d sec" % (time.time() - start))

    return nfeat
Ejemplo n.º 24
0
    def testWriteWithBinaryField(self):
        """
        Test writing with a binary field
        :return:
        """
        basetestpath = tempfile.mkdtemp()

        tmpfile = os.path.join(basetestpath, 'binaryfield.sqlite')
        ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile)
        lyr = ds.CreateLayer('test',
                             geom_type=ogr.wkbPoint,
                             options=['FID=fid'])
        lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString))
        lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger))
        lyr.CreateField(ogr.FieldDefn('binfield', ogr.OFTBinary))
        lyr.CreateField(ogr.FieldDefn('binfield2', ogr.OFTBinary))
        f = None
        ds = None

        vl = QgsVectorLayer(tmpfile)
        self.assertTrue(vl.isValid())

        # check that 1 of its fields is a bool
        fields = vl.fields()
        self.assertEqual(
            fields.at(fields.indexFromName('binfield')).type(),
            QVariant.ByteArray)

        dp = vl.dataProvider()
        f = QgsFeature(fields)
        bin_1 = b'xxx'
        bin_2 = b'yyy'
        bin_val1 = QByteArray(bin_1)
        bin_val2 = QByteArray(bin_2)
        f.setAttributes([1, 'str', 100, bin_val1, bin_val2])
        self.assertTrue(dp.addFeature(f))

        # write a gpkg package with a binary field
        filename = os.path.join(str(QDir.tempPath()), 'with_bin_field')
        rc, errmsg = QgsVectorFileWriter.writeAsVectorFormat(
            vl, filename, 'utf-8', vl.crs(), 'GPKG')

        self.assertEqual(rc, QgsVectorFileWriter.NoError)

        # open the resulting geopackage
        vl = QgsVectorLayer(filename + '.gpkg', '', 'ogr')
        self.assertTrue(vl.isValid())
        fields = vl.fields()

        # test type of converted field
        idx = fields.indexFromName('binfield')
        self.assertEqual(fields.at(idx).type(), QVariant.ByteArray)
        idx2 = fields.indexFromName('binfield2')
        self.assertEqual(fields.at(idx2).type(), QVariant.ByteArray)

        # test values
        self.assertEqual(vl.getFeature(1).attributes()[idx], bin_val1)
        self.assertEqual(vl.getFeature(1).attributes()[idx2], bin_val2)

        del vl
        os.unlink(filename + '.gpkg')
Ejemplo n.º 25
0
    def processAlgorithm(self, parameters, context, feedback):
        raster_layer = self.parameterAsRasterLayer(parameters, self.INPUT_DEM, context)
        target_crs = raster_layer.crs()
        rasterPath = raster_layer.source()

        source = self.parameterAsSource(parameters, self.BOUNDARY_LAYER, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.BOUNDARY_LAYER))

        step = self.parameterAsDouble(parameters, self.STEP, context)
        percentage = self.parameterAsBool(parameters, self.USE_PERCENTAGE, context)

        outputPath = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context)

        rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
        geoTransform = rasterDS.GetGeoTransform()
        rasterBand = rasterDS.GetRasterBand(1)
        noData = rasterBand.GetNoDataValue()

        cellXSize = abs(geoTransform[1])
        cellYSize = abs(geoTransform[5])
        rasterXSize = rasterDS.RasterXSize
        rasterYSize = rasterDS.RasterYSize

        rasterBBox = QgsRectangle(geoTransform[0],
                                  geoTransform[3] - cellYSize * rasterYSize,
                                  geoTransform[0] + cellXSize * rasterXSize,
                                  geoTransform[3])
        rasterGeom = QgsGeometry.fromRect(rasterBBox)

        crs = osr.SpatialReference()
        crs.ImportFromProj4(str(target_crs.toProj4()))

        memVectorDriver = ogr.GetDriverByName('Memory')
        memRasterDriver = gdal.GetDriverByName('MEM')

        features = source.getFeatures(QgsFeatureRequest().setDestinationCrs(target_crs, context.transformContext()))
        total = 100.0 / source.featureCount() if source.featureCount() else 0

        for current, f in enumerate(features):
            if not f.hasGeometry():
                continue

            if feedback.isCanceled():
                break

            geom = f.geometry()
            intersectedGeom = rasterGeom.intersection(geom)

            if intersectedGeom.isEmpty():
                feedback.pushInfo(
                    self.tr('Feature {0} does not intersect raster or '
                            'entirely located in NODATA area').format(f.id()))
                continue

            fName = os.path.join(
                outputPath, 'hystogram_%s_%s.csv' % (source.sourceName(), f.id()))

            ogrGeom = ogr.CreateGeometryFromWkt(intersectedGeom.asWkt())
            bbox = intersectedGeom.boundingBox()
            xMin = bbox.xMinimum()
            xMax = bbox.xMaximum()
            yMin = bbox.yMinimum()
            yMax = bbox.yMaximum()

            (startColumn, startRow) = raster.mapToPixel(xMin, yMax, geoTransform)
            (endColumn, endRow) = raster.mapToPixel(xMax, yMin, geoTransform)

            width = endColumn - startColumn
            height = endRow - startRow

            srcOffset = (startColumn, startRow, width, height)
            srcArray = rasterBand.ReadAsArray(*srcOffset)

            if srcOffset[2] == 0 or srcOffset[3] == 0:
                feedback.pushInfo(
                    self.tr('Feature {0} is smaller than raster '
                            'cell size').format(f.id()))
                continue

            newGeoTransform = (
                geoTransform[0] + srcOffset[0] * geoTransform[1],
                geoTransform[1],
                0.0,
                geoTransform[3] + srcOffset[1] * geoTransform[5],
                0.0,
                geoTransform[5]
            )

            memVDS = memVectorDriver.CreateDataSource('out')
            memLayer = memVDS.CreateLayer('poly', crs, ogr.wkbPolygon)

            ft = ogr.Feature(memLayer.GetLayerDefn())
            ft.SetGeometry(ogrGeom)
            memLayer.CreateFeature(ft)
            ft.Destroy()

            rasterizedDS = memRasterDriver.Create('', srcOffset[2],
                                                  srcOffset[3], 1, gdal.GDT_Byte)
            rasterizedDS.SetGeoTransform(newGeoTransform)
            gdal.RasterizeLayer(rasterizedDS, [1], memLayer, burn_values=[1])
            rasterizedArray = rasterizedDS.ReadAsArray()

            srcArray = numpy.nan_to_num(srcArray)
            masked = numpy.ma.MaskedArray(srcArray,
                                          mask=numpy.logical_or(srcArray == noData,
                                                                numpy.logical_not(rasterizedArray)))

            self.calculateHypsometry(f.id(), fName, feedback, masked,
                                     cellXSize, cellYSize, percentage, step)

            memVDS = None
            rasterizedDS = None
            feedback.setProgress(int(current * total))

        rasterDS = None

        return {self.OUTPUT_DIRECTORY: outputPath}
Ejemplo n.º 26
0
def Convert_Shapefile(options):

    #  Log Entry
    logging.debug('Converting Shapefile')

    #  Get the Input Spatial Reference
    in_srs = options.Get_Input_SRS()
    out_srs = options.Get_Output_SRS()

    #  Set the driver
    driver = ogr.GetDriverByName('ESRI Shapefile')

    # Open the File
    dataset = driver.Open(options.config['shapefile_path'], gdal.GA_ReadOnly)

    #  Make sure it succeeded
    if dataset is None:
        raise Exception('Unable to open the Shapefile (' +
                        options.config['shapefile_path'] + ')')

    #  Log Driver Info
    logging.debug('Opened Shapefile: ' + str(options.config['shapefile_path']))
    logging.debug(' - Driver    : ' + str(driver.GetName()))
    logging.debug(' - Input SRS : ' + str(in_srs.ExportToProj4()))
    logging.debug(' - Output SRS: ' + str(out_srs.ExportToProj4()))

    #  Construct the Coordinate Transform
    crd_xform = osr.CoordinateTransformation(in_srs, out_srs)

    #  Fetch each layer
    layer_cnt = dataset.GetLayerCount()
    logging.debug(' - Detected ' + str(layer_cnt) + ' layers.')
    for lidx in range(layer_cnt):

        #  Grab the layer
        layer = dataset.GetLayerByIndex(lidx)

        #  Grab the layer definition
        feature_def = layer.GetLayerDefn()

        #  Iterate over features
        for feature in layer:

            #  Get the feature count
            feature_count = feature.GetGeomFieldCount()
            for fidx in range(feature_count):

                #  Get the geometry
                geom = feature.GetGeomFieldRef(fidx)

                #  Get the geometry components
                field_defn = feature_def.GetFieldDefn(fidx)

                #   Process Point
                if geom is not None and geom.GetGeometryType() == ogr.wkbPoint:
                    print "%.3f, %.3f" % (geom.GetX(), geom.GetY())

                #  Process Polygon
                elif geom is not None and geom.GetGeometryType(
                ) == ogr.wkbPolygon:

                    #  Get the Geometry Count
                    geo_count = geom.GetGeometryCount()
                    pnt_count = geom.GetPointCount()

                    for gidx in range(geo_count):

                        #  Get the geometry
                        igeom = geom.GetGeometryRef(gidx)
                        ipnt = igeom.GetPointCount()
                        print('\nSub-Geometry: ' + str(gidx))
                        print('Sub-Point Cnt: ' + str(ipnt))

                        #  Print each point
                        for pidx in range(ipnt):

                            tp = igeom.GetPoint_2D(pidx)
                            print("Point " + str(pidx) + ': ' + str(tp))

                else:
                    print "no point geometry\n"

                #  Print stuff
                print('Feature Idx: ' + str(fidx))
                print('  Def: ' + str(field_defn))
                print('  Geometry Type: ' + str(geom.GetGeometryType()) +
                      ' vs ' + str(ogr.wkbPolygon))
                raw_input('pause')
Ejemplo n.º 27
0
    def generate_download_bundle(self, tables, geos, geo_ids, release, columns,
                                 data, fmt):
        if not HAS_GDAL:
            gdal_missing(critical=True)

        from osgeo import ogr, osr
        self.ogr = ogr
        self.osr = osr
        ogr.UseExceptions()

        format = self.DOWNLOAD_FORMATS[fmt]

        # where we're going to put the data temporarily
        temp_path = tempfile.mkdtemp()
        try:
            file_ident = "%s_%s_%s" % (
                tables[0].name.upper(),
                # The gdal KML driver doesn't like certain chars in its layer names.
                # It will replace them for you, but then subsequent calls hang.
                self.BAD_LAYER_CHARS.sub('_',
                                         release.name + '_' + release.year),
                self.BAD_LAYER_CHARS.sub('_', geos[0].name))

            # where the files go, what we'll eventually zip up
            inner_path = os.path.join(temp_path, file_ident)
            log.info("Generating download in %s" % inner_path)
            os.mkdir(inner_path)
            out_filepath = os.path.join(inner_path,
                                        '%s.%s' % (file_ident, fmt))

            out_driver = ogr.GetDriverByName(format['driver'])
            out_srs = osr.SpatialReference()
            out_srs.ImportFromEPSG(4326)
            out_data = out_driver.CreateDataSource(out_filepath)

            # See http://gis.stackexchange.com/questions/53920/ogr-createlayer-returns-typeerror
            # excel limits worksheet names to 31 chars
            out_layer = out_data.CreateLayer(file_ident[0:31],
                                             srs=out_srs,
                                             geom_type=ogr.wkbMultiPolygon)
            out_layer.CreateField(ogr.FieldDefn('geo_level', ogr.OFTString))
            out_layer.CreateField(ogr.FieldDefn('geo_code', ogr.OFTString))
            out_layer.CreateField(ogr.FieldDefn('geoid', ogr.OFTString))
            out_layer.CreateField(ogr.FieldDefn('name', ogr.OFTString))

            for table in tables:
                for column_id, column_info in columns[table.name].items():
                    out_layer.CreateField(
                        ogr.FieldDefn(str(column_id), ogr.OFTReal))

            for geo in geos:
                geoid = geo.geoid

                out_feat = ogr.Feature(out_layer.GetLayerDefn())

                if format['geometry']:
                    geom = self.get_geometry(geo)
                    if geom:
                        out_feat.SetGeometry(geom)

                out_feat.SetField2('geo_level', geo.geo_level)
                out_feat.SetField2('geo_code', geo.geo_code)
                out_feat.SetField2('geoid', geoid)
                out_feat.SetField2('name', geo.name.encode('utf-8'))

                for table in tables:
                    table_estimates = data[geoid][
                        table.name.upper()]['estimate']

                    for column_id, column_info in columns[table.name].items():
                        if column_id in table_estimates:
                            est = table_estimates[column_id]
                            # None values get changed to zero, which isn't accurate
                            if est is None:
                                continue

                            # GDAL generates invalid excel spreadsheets for
                            # zero values in real columns
                            if fmt == 'xlsx' and est == 0:
                                continue
                            out_feat.SetField(str(column_id), est)

                out_layer.CreateFeature(out_feat)

            # this closes the object and ensure
            # the data is flushed to the file
            out_data = None

            # zip it up, they can be huge
            zfile_filename = file_ident + '.zip'
            zfile_filepath = os.path.join(temp_path, zfile_filename)
            log.info("Zipping download into %s" % zfile_filepath)

            zfile = zipfile.ZipFile(zfile_filepath, 'w', zipfile.ZIP_DEFLATED)
            for root, dirs, files in os.walk(inner_path):
                for f in files:
                    zfile.write(os.path.join(root, f),
                                os.path.join(file_ident, f))
            zfile.close()

            log.info("Zipped. Reading and streaming.")

            with open(zfile_filepath, "rb") as f:
                content = f.read()
                return content, zfile_filename, 'application/zip'

        finally:
            shutil.rmtree(temp_path)
Ejemplo n.º 28
0
def bound_raster(fileNameXML, listMeta):
    """
    Try open file TIFF or TIF file, if its imposible write ERROR
    If file have not metadata sys.exit(1)
    When this function is done, it will be cut to form a new shapefile,
    by calculating the intersection of the outline of the image formed from the shapefile and metadata.
    Yes, they do not match!
    Create new shapefile by ogr tools
    :param fileNameXML: name XML file
    :param listMeta: list of metadata for this file
    :return: NONE!
    """

    try:
        try:
            fileName = fileNameXML.replace('.xml','.tiff')
            # open dataset
            dataset = gdal.Open(fileName)
            print u'Драйвер: ', dataset.GetDriver().ShortName, u'/', dataset.GetDriver().LongName
            print u'Размер ',dataset.RasterXSize, u'x', dataset.RasterYSize, u'x', dataset.RasterCount
            print u'Проекция ', dataset.GetProjection()
            geotransform = dataset.GetGeoTransform()
            if not geotransform is None:
                print u'Начало координат (',geotransform[0], u',',geotransform[3],u')'
                print u'Размер пиксела = (',geotransform[1], u',',geotransform[5],u')'

        except:
            fileName = fileNameXML.replace('.xml','.tiff')
            # open dataset
            dataset = gdal.Open(fileName)
            print u'Драйвер: ', dataset.GetDriver().ShortName, u'/', dataset.GetDriver().LongName
            print u'Размер ',dataset.RasterXSize, u'x', dataset.RasterYSize, u'x', dataset.RasterCount
            print u'Проекция ', dataset.GetProjection()
            geotransform = dataset.GetGeoTransform()
            if not geotransform is None:
                print u'Начало координат (',geotransform[0], u',',geotransform[3],u')'
                print u'Размер пиксела = (',geotransform[1], u',',geotransform[5],u')'


    except:
        print u"Error file format"
        sys.exit(1)


    if not geotransform[0]==0:
        if not geotransform[3]==0:
            # Get cord of corners for points shapefiles
            newXA = geotransform[0]
            newYA = geotransform[3]
            print newXA, newYA


            newXB = geotransform[0] + dataset.RasterXSize*geotransform[1]
            newYB = geotransform[3]
            print newXB, newYB

            newXC = geotransform[0] + dataset.RasterXSize*geotransform[1]
            newYC = geotransform[3] + dataset.RasterYSize*geotransform[5]
            print newXC, newYC

            newXD = geotransform[0]
            newYD = geotransform[3] + dataset.RasterYSize*geotransform[5]
            print newXD, newYD

            # Create ring from points corner raster
            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint_2D(newXA, newYA)
            ring.AddPoint_2D(newXB, newYB)
            ring.AddPoint_2D(newXC, newYC)
            ring.AddPoint_2D(newXD, newYD)
            ring.AddPoint_2D(newXA, newYA)

            # Create polygon
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)

            print poly.ExportToWkt()


            file = open(fileNameXML)
            data = file.read()
            file.close()
            dom = parseString(data)

            # Cordinats Tag. Check coord system
            coordCodeTag = dom.getElementsByTagName('nCoordSystCode')[0].toxml()
            coordCode = coordCodeTag.replace('<nCoordSystCode>','').replace('</nCoordSystCode>','')

            # Change prjection FROM
            source = osr.SpatialReference() #https://pcjericks.github.io/py-gdalogr-cookbook/projection.html#reproject-a-geometry
            source.ImportFromEPSG(int(coordCode))

            # Change prjection TO
            target = osr.SpatialReference()
            target.ImportFromEPSG(3857)

            transform = osr.CoordinateTransformation(source, target)

            poly.Transform(transform)
            print poly.ExportToWkt()
            print u"POLY:",poly
            #-----------------------------------------------------------------------

            (head, tail) = os.path.split(fileNameXML)
            print tail

            # Get a Layer's Extent
            inShapefile = targetFolder + "/shapefiles/" + tail.replace('.xml', '.shp')
            print inShapefile
            inDriver = ogr.GetDriverByName("ESRI Shapefile")
            inDataSource = inDriver.Open(inShapefile, 0)
            inLayer = inDataSource.GetLayer()
            for feature in inLayer:
                geom = feature.GetGeometryRef()
                print u"InSHP>>:"+geom.ExportToWkt()

            layerSpaFil = geom.Intersection(poly)

            print u"RESULT:",layerSpaFil.ExportToWkt()

            # Save to a new Shapefile
            outShapefile = targetFolder + "/shapefiles//" + tail.replace('.xml', '_c.shp')
            print outShapefile
            outDriver = ogr.GetDriverByName("ESRI Shapefile")

            # Remove output shapefile if it already exists
            if os.path.exists(outShapefile):
                outDriver.DeleteDataSource(outShapefile)

            outDataSource = outDriver.CreateDataSource(outShapefile)
            outLayer = outDataSource.CreateLayer("spatial", geom_type=ogr.wkbPolygon)

            # Add fields (create with ogr type)
            outLayer.CreateField(ogr.FieldDefn("ogc_fid", ogr.OFTInteger))
            outLayer.CreateField(ogr.FieldDefn("id", ogr.OFTInteger))
            outLayer.CreateField(ogr.FieldDefn("session_ti", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("ka", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("device", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("channels_c", ogr.OFTInteger))
            outLayer.CreateField(ogr.FieldDefn("channels", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("resolution", ogr.OFTReal))
            outLayer.CreateField(ogr.FieldDefn("cloud_cove", ogr.OFTInteger))
            outLayer.CreateField(ogr.FieldDefn("sun_angle", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("name", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("proc_level", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("conditions", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("history_or", ogr.OFTString))
            outLayer.CreateField(ogr.FieldDefn("projects_c", ogr.OFTString))

            featureDefn = outLayer.GetLayerDefn()
            feature = ogr.Feature(featureDefn)

            #add data
            feature.SetField("ogc_fid", listMeta[0])
            feature.SetField("id", listMeta[1])
            feature.SetField("session_ti", listMeta[2])
            feature.SetField("ka", listMeta[3])
            feature.SetField("device", listMeta[4])
            feature.SetField("channels_c", listMeta[5])
            feature.SetField("channels", listMeta[6])
            feature.SetField("resolution", listMeta[7])
            feature.SetField("cloud_cove", listMeta[8])
            feature.SetField("sun_angle", listMeta[9])
            feature.SetField("name", listMeta[10])
            feature.SetField("proc_level", listMeta[11])
            feature.SetField("conditions", listMeta[12])
            feature.SetField("history_or", listMeta[13])
            feature.SetField("projects_c", listMeta[14])

            feature.SetGeometry(layerSpaFil)
            outLayer.CreateFeature(feature)

            geo = layerSpaFil.ExportToWkt()
            print u">>>> "+geo

    # For Resurs-P only, if no geo
    (head, tail) = os.path.split(fileNameXML)
    inShapefile = targetFolder + "/shapefiles/" + tail.replace('.xml', '.shp')
    print inShapefile
    inDriver = ogr.GetDriverByName("ESRI Shapefile")
    inDataSource = inDriver.Open(inShapefile, 0)
    inLayer = inDataSource.GetLayer()
    featureDefn = inLayer.GetLayerDefn()
    feature = ogr.Feature(featureDefn)
    layerSpaFil = inShapefile
    print u"layerSpaFil >>> "+layerSpaFil
    for feature in inLayer:
        geom = feature.GetGeometryRef()
        geo = geom.ExportToWkt()
        print u"InSHP>>:"+geo
    #geo = geom.ExportToWkt()
    #print u">>>> "+geom

    dbData = parce_db_data (u"D:\SUAI\PyCharm\BD_spiiras.xml")
    print "dbname="+dbData[0]+" host=:"+dbData[1]+" port="+dbData[2]+" user="******" password="******"dbname="+dbData[0]+" host="+dbData[1]+" port="+dbData[2]+" user="******"password="******"connecting is OK!"
    except:
        print "I am unable to connect to the database"
        sys.exit(1)

    cur = conn.cursor()

    sql = '''INSERT INTO public.spc_vector_contur (geom, ogc_fid, id, session_ti, ka, device, channels_c, channels, resolution, cloud_cove, sun_angle, name, proc_level, conditions, history_or, projects_c) VALUES (ST_GeomFromText(%s, 3857), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
    param = [geo, listMeta[0], listMeta[1], listMeta[2], listMeta[3], listMeta[4], listMeta[5], listMeta[6], listMeta[7], listMeta[8], listMeta[9], listMeta[10], listMeta[11], listMeta[12], listMeta[13], listMeta[14]]
    print param

    cur.execute(sql, param)
    conn.commit()

    cur.close()
    conn.close()

    feature.Destroy()
    inDataSource.Destroy()
Ejemplo n.º 29
0
def codigo_ine(lon, lat, nombre, num_carac, simple=False):

    if num_carac != 9:
        if simple:
            return ''
        else:
            return '', float('NaN'), float('NaN'), float('NaN')

    (r, tita, z) = codigo_ine_wgs84(lon, lat)

    if simple:
        return r

    ## Generar un SHP con esta franja
    drv = ogr.GetDriverByName("ESRI Shapefile")
    outputfile = "../resultados/INE_" + nombre + ".shp"
    if os.path.exists(outputfile):
        drv.DeleteDataSource(outputfile)
    output = drv.CreateDataSource(outputfile)
    franja = output.CreateLayer("Franja", geom_type=ogr.wkbPolygon)

    lambert = osr.SpatialReference()
    lambert.ImportFromProj4(
        "+proj=lcc +lat_1=-11.5 +lat_2=-21.5 +lat_0=-24 +lon_0=-64 +x_0=1000000 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
    )
    wgs84 = osr.SpatialReference()
    wgs84.ImportFromEPSG(4326)
    transfWGS84 = osr.CoordinateTransformation(lambert, wgs84)

    ## Calcular la franja de territorio con el mismo codigo que el punto
    mintita = min(vtita)
    maxtita = max(vtita)
    paso_tita = (maxtita - mintita) / 1000
    vrmin = []
    vptmin = []
    vrmax = []
    vptmax = []
    vdeltar = []
    for i in range(0, 999):
        tita = mintita + i * paso_tita
        rmin = z * math.exp(-tita)
        rmax = (z + 1) * math.exp(-tita)
        vrmin.append(rmin)
        vptmin.append((rmin, tita))
        vrmax.append(rmax)
        vptmax.append((rmax, tita))
        vdeltar.append(rmax - rmin)

    vpt = vptmin
    vpt.extend(reversed(vptmax))

    ring = ogr.Geometry(ogr.wkbLinearRing)
    for i in vpt:
        x_franja = i[0] * math.cos(i[1] - math.pi)
        y_franja = i[0] * math.sin(i[1] - math.pi)
        ring.AddPoint(x_franja, y_franja)

    ring.CloseRings()

    poly = ogr.Geometry(ogr.wkbPolygon)
    poly.AddGeometry(ring)

    poly_intersection = poly.Intersection(poly_bolivia_lambert)

    superficie = poly_intersection.GetArea()
    largo = poly_intersection.Boundary().Length() / 2
    ancho = float(sum(vdeltar)) / len(vdeltar)
    ancho_min = min(vdeltar)
    ancho_max = max(vdeltar)
    print "************************************************"
    print "Codigo INE z - " + nombre + ":"
    print " * valor: %d" % z
    print " * area equi-codigo: arco de %gm2, %gm de largo por %gm de ancho promedio (min=%gm max=%gm)" % (
        superficie, largo, ancho, ancho_min, ancho_max)
    print "************************************************"

    poly_intersection.Transform(transfWGS84)
    f = ogr.Feature(feature_def=franja.GetLayerDefn())
    f.SetGeometryDirectly(poly_intersection)
    franja.CreateFeature(f)
    output.Destroy()

    return z, superficie, largo, ancho
Ejemplo n.º 30
0
    def processAlgorithm(self, progress):
        rasterPath = self.getParameterValue(self.INPUT_DEM)
        layer = dataobjects.getObjectFromUri(
            self.getParameterValue(self.BOUNDARY_LAYER))
        step = self.getParameterValue(self.STEP)
        percentage = self.getParameterValue(self.USE_PERCENTAGE)

        outputPath = self.getOutputValue(self.OUTPUT_DIRECTORY)

        rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
        geoTransform = rasterDS.GetGeoTransform()
        rasterBand = rasterDS.GetRasterBand(1)
        noData = rasterBand.GetNoDataValue()

        cellXSize = abs(geoTransform[1])
        cellYSize = abs(geoTransform[5])
        rasterXSize = rasterDS.RasterXSize
        rasterYSize = rasterDS.RasterYSize

        rasterBBox = QgsRectangle(geoTransform[0],
                                  geoTransform[3] - cellYSize * rasterYSize,
                                  geoTransform[0] + cellXSize * rasterXSize,
                                  geoTransform[3])
        rasterGeom = QgsGeometry.fromRect(rasterBBox)

        crs = osr.SpatialReference()
        crs.ImportFromProj4(str(layer.crs().toProj4()))

        memVectorDriver = ogr.GetDriverByName('Memory')
        memRasterDriver = gdal.GetDriverByName('MEM')

        features = vector.features(layer)
        count = len(features)
        total = 100.0 / float(count)

        for count, f in enumerate(features):
            geom = f.geometry()
            intersectedGeom = rasterGeom.intersection(geom)

            if intersectedGeom.isGeosEmpty():
                progress.setInfo(
                    self.tr('Feature %d does not intersect raster or '
                            'entirely located in NODATA area' % f.id()))
                continue

            fName = os.path.join(
                outputPath, 'hystogram_%s_%s.csv' % (layer.name(), f.id()))

            ogrGeom = ogr.CreateGeometryFromWkt(intersectedGeom.exportToWkt())
            bbox = intersectedGeom.boundingBox()
            xMin = bbox.xMinimum()
            xMax = bbox.xMaximum()
            yMin = bbox.yMinimum()
            yMax = bbox.yMaximum()

            (startColumn,
             startRow) = raster.mapToPixel(xMin, yMax, geoTransform)
            (endColumn, endRow) = raster.mapToPixel(xMax, yMin, geoTransform)

            width = endColumn - startColumn
            height = endRow - startRow

            srcOffset = (startColumn, startRow, width, height)
            srcArray = rasterBand.ReadAsArray(*srcOffset)

            if srcOffset[2] == 0 or srcOffset[3] == 0:
                progress.setInfo(
                    self.tr('Feature %d is smaller than raster '
                            'cell size' % f.id()))
                continue

            newGeoTransform = (geoTransform[0] +
                               srcOffset[0] * geoTransform[1], geoTransform[1],
                               0.0, geoTransform[3] +
                               srcOffset[1] * geoTransform[5], 0.0,
                               geoTransform[5])

            memVDS = memVectorDriver.CreateDataSource('out')
            memLayer = memVDS.CreateLayer('poly', crs, ogr.wkbPolygon)

            ft = ogr.Feature(memLayer.GetLayerDefn())
            ft.SetGeometry(ogrGeom)
            memLayer.CreateFeature(ft)
            ft.Destroy()

            rasterizedDS = memRasterDriver.Create('', srcOffset[2],
                                                  srcOffset[3], 1,
                                                  gdal.GDT_Byte)
            rasterizedDS.SetGeoTransform(newGeoTransform)
            gdal.RasterizeLayer(rasterizedDS, [1], memLayer, burn_values=[1])
            rasterizedArray = rasterizedDS.ReadAsArray()

            srcArray = numpy.nan_to_num(srcArray)
            masked = numpy.ma.MaskedArray(
                srcArray,
                mask=numpy.logical_or(srcArray == noData,
                                      numpy.logical_not(rasterizedArray)))

            self.calculateHypsometry(f.id(), fName, progress, masked,
                                     cellXSize, cellYSize, percentage, step)

            memVDS = None
            rasterizedDS = None
            progress.setPercentage(int(count * total))

        rasterDS = None