Exemplo n.º 1
0
def ogr_wkbwkt_test_geometrycollection_wkb_recursion():

    import struct
    wkb_repeat = struct.pack('B' * 9, 0, 0, 0, 0, 7, 0, 0, 0, 1 )
    wkb_end = struct.pack('B' * 9, 0, 0, 0, 0, 7, 0, 0, 0, 0 )

    wkb = struct.pack('B' * 0)
    for i in range(31):
        wkb = wkb + wkb_repeat
    wkb = wkb + wkb_end

    geom = ogr.CreateGeometryFromWkb(wkb)
    if geom is None:
        gdaltest.post_reason('expected a geometry')
        return 'fail'

    wkb = struct.pack('B' * 0)
    for i in range(32):
        wkb = wkb + wkb_repeat
    wkb = wkb + wkb_end

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    geom = ogr.CreateGeometryFromWkb(wkb)
    gdal.PopErrorHandler()
    if geom is not None:
        gdaltest.post_reason('expected None')
        return 'fail'

    return 'success'
Exemplo n.º 2
0
 def way(self, w):
     if len(w.tags) == 0:
         return
     if w.is_closed() and closed_way_is_polygon(
             w.tags):  # this will be handled in area()
         return
     try:
         # NOTE: it is possible this is actually a MultiLineString
         # in the case where a LineString is clipped by the clipping geom,
         # or the way is self-intersecting
         # but GDAL and QGIS seem to handle it OK.
         linestring = None
         for theme in self.mapping.themes:
             if theme.matches(GeomType.LINE, w.tags):
                 if not linestring:
                     wkb = fab.create_linestring(w)
                     if self.clipping_geom:
                         sg = loads(bytes.fromhex(wkb))
                         if not self.prepared_clipping_geom.intersects(sg):
                             return
                         if not self.prepared_clipping_geom.contains_properly(
                                 sg):
                             sg = self.clipping_geom.intersection(sg)
                         linestring = ogr.CreateGeometryFromWkb(dumps(sg))
                     else:
                         linestring = create_geom(wkb)
                 for output in self.outputs:
                     output.write(w.id, theme.name, GeomType.LINE,
                                  linestring, w.tags)
     except RuntimeError:
         print("Incomplete way: {0}".format(w.id))
Exemplo n.º 3
0
        def netgeometry(key, data):
            if 'Wkb' in data:
                geom = ogr.CreateGeometryFromWkb(data['Wkb'])
            elif 'Wkt' in data:
                geom = ogr.CreateGeometryFromWkt(data['Wkt'])
            elif type(
                    key[0]).__name__ == 'tuple':  # edge keys are packed tuples
                geom = ogr.Geometry(ogr.wkbLineString)
                _from, _to = key[0], key[1]
                try:
                    geom.SetPoint(0, *_from)
                    geom.SetPoint(1, *_to)
                except TypeError:
                    # assume user used tuple of int and choked ogr
                    _ffrom = [float(x) for x in _from]
                    _fto = [float(x) for x in _to]
                    geom.SetPoint(0, *_ffrom)
                    geom.SetPoint(1, *_fto)
            else:
                geom = ogr.Geometry(ogr.wkbPoint)
                try:
                    geom.SetPoint(0, *key)
                except TypeError:
                    # assume user used tuple of int and choked ogr
                    fkey = [float(x) for x in key]
                    geom.SetPoint(0, *fkey)

            return geom
Exemplo n.º 4
0
def save(polygones, name, epsg):
    driver = ogr.GetDriverByName('Esri Shapefile')
    ds = driver.CreateDataSource(name)

    srs = ogr.osr.SpatialReference()
    srs.ImportFromEPSG(epsg)

    geom = polygones[(0, 0)]  # (0, 0) object is presented always
    if geom.geom_type == 'Polygon':
        geom_type = ogr.wkbPolygon
    elif geom.geom_type == 'LineString':
        geom_type = ogr.wkbLineString
    else:
        raise NotImplementedError("Unknown geometry type %s" %
                                  (geom.geom_type, ))

    layer = ds.CreateLayer('', srs, geom_type)
    layer.CreateField(ogr.FieldDefn('col', ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn('row', ogr.OFTInteger))
    defn = layer.GetLayerDefn()

    for (col, row), poly in polygones.items():
        feat = ogr.Feature(defn)
        feat.SetField('col', col)
        feat.SetField('row', row)

        geom = ogr.CreateGeometryFromWkb(poly.wkb)
        feat.SetGeometry(geom)

        layer.CreateFeature(feat)
        feat = geom = None

    ds = layer = feat = geom = None
Exemplo n.º 5
0
 def area(self, a):
     if len(a.tags) == 0:
         return
     if not closed_way_is_polygon(a.tags):
         return
     osm_id = a.orig_id() if a.from_way() else -a.orig_id()
     try:
         multipolygon = None
         for theme in self.mapping.themes:
             if theme.matches(GeomType.POLYGON, a.tags):
                 if not multipolygon:
                     wkb = fab.create_multipolygon(a)
                     if self.clipping_geom:
                         sg = loads(bytes.fromhex(wkb))
                         if not self.prepared_clipping_geom.intersects(sg):
                             return
                         if not self.prepared_clipping_geom.contains_properly(
                                 sg):
                             sg = self.clipping_geom.intersection(sg)
                         multipolygon = ogr.CreateGeometryFromWkb(dumps(sg))
                     else:
                         multipolygon = create_geom(wkb)
                 for output in self.outputs:
                     output.write(osm_id, theme.name, GeomType.POLYGON,
                                  multipolygon, a.tags)
     except RuntimeError:
         print('Invalid area: {0}'.format(a.orig_id()))
Exemplo n.º 6
0
def ogr_db2_hack_3():

    if ogr.SetGenerate_DB2_V72_BYTE_ORDER(1) != 0:
        return 'skip'

    wkt = 'MULTIPOLYGON (((10.00121344 2.99853145,10.00121344 1.99853145,11.00121343 1.99853148,11.00121343 2.99853148)),((10.00121344 2.99853145,10.00121344 3.99853145,9.00121345 3.99853143,9.00121345 2.99853143)))'

    geom = ogr.CreateGeometryFromWkt(wkt)
    wkb = geom.ExportToWkb()
    geom.Destroy()

    # Check primary byte order value.
    if wkb.decode('latin1')[0] != '0' and wkb.decode('latin1')[0] != '1':
        gdaltest.post_reason('corrupt primary geometry byte order')
        return 'fail'

    # Check component geometry byte order
    if wkb.decode('latin1')[9] != '0' and wkb.decode('latin1')[9] != '1':
        gdaltest.post_reason('corrupt sub-geometry byte order')
        return 'fail'

    geom = ogr.CreateGeometryFromWkb(wkb)
    if geom.ExportToWkt() != wkt:
        gdaltest.post_reason(
            'Convertion to/from DB2 format seems to have corrupted geometry.')
        return 'fail'

    geom.Destroy()

    ogr.SetGenerate_DB2_V72_BYTE_ORDER(0)

    return 'success'
Exemplo n.º 7
0
    def save_shapefile(self,
                       path,
                       name,
                       geom_wkb_dict,
                       gtype=ogr.wkbMultiPolygon):
        """ Save a collection of geometries to a shapefile in [path].
        :param geom_wkb_dict: dict (key, value) = (identifier, wkb (binary string))"""
        os.popen("mkdir -p {}".format(path))

        # Now convert it to a shapefile with OGR
        driver = ogr.GetDriverByName('Esri Shapefile')
        ds = driver.CreateDataSource(os.path.join(path, '{}.shp'.format(name)))
        layer = ds.CreateLayer('', None, gtype)

        # Add one attribute
        layer.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
        defn = layer.GetLayerDefn()

        for key, wkb in geom_wkb_dict.iteritems():
            # Create a new feature (attribute and geometry)
            feat = ogr.Feature(defn)
            feat.SetField('id', 123)

            # Make a geometry, from Shapely object
            geom = ogr.CreateGeometryFromWkb(wkb)
            feat.SetGeometry(geom)

            layer.CreateFeature(feat)
            feat = geom = None  # destroy these

        # Save and close everything
        ds = layer = feat = geom = None
Exemplo n.º 8
0
def write_polylines(poly, out_file):
    """Write polyline shapefile to file path.

    :param poly: input polyline
    :type poly: shapely polyline object
    :param out_file: file path to restore polyline
    :type out_file: str
    """
    driver = ogr.GetDriverByName("Esri Shapefile")
    ds = driver.CreateDataSource(out_file)
    layer = ds.CreateLayer("", None, ogr.wkbLineString)
    # Add one attribute
    layer.CreateField(ogr.FieldDefn("id", ogr.OFTInteger))
    defn = layer.GetLayerDefn()

    # Create a new feature (attribute and geometry)
    feat = ogr.Feature(defn)
    feat.SetField("id", 1)

    # Make a geometry, from Shapely object
    geom = ogr.CreateGeometryFromWkb(poly.wkb)
    feat.SetGeometry(geom)

    layer.CreateFeature(feat)

    # Save and close everything
    ds = layer = feat = geom = None
Exemplo n.º 9
0
def clip(in_lyr, clip_lyr=None, clip_feat=None, outName=None):

    import ogr
    import os

    if clip_lyr is None and clip_feat is None:
        print('No clip layer or clip feature defined. Exiting')
        return

    out_ds = ogr.GetDriverByName('Memory').CreateDataSource('mem')
    out_lyr = out_ds.CreateLayer('')

    clipper = ogr.GetDriverByName('Memory').CreateDataSource('mem')
    clipper_lyr = clipper.CreateLayer('')
    defn = clipper_lyr.GetLayerDefn()
    out_feat = ogr.Feature(defn)
    if clip_feat is None:
        clip_feat = clip_lyr.GetNextFeature()
    clip_geom = clip_feat.geometry().Clone()
    clip_geom_Wkb = ogr.CreateGeometryFromWkb(clip_geom.ExportToWkb())
    out_feat.SetGeometry(clip_geom_Wkb)
    clipper_lyr.CreateFeature(out_feat)

    ogr.Layer.Clip(in_lyr, clipper_lyr, out_lyr)

    if outName is not None:
        write_vector(out_ds, outName, srs=get_srs_vec(in_lyr))

    return out_ds
Exemplo n.º 10
0
def makeValidByOGR(gdf, verbose=False):
    '''Returns a copy of the geodataframe (or list thereof) passed through ogr MakeValid
    
    Requires geopandas, shapely, ogr
    '''
    unlist = False
    if type(gdf) != list:
        gdf = [gdf]
        unlist = True
    ret = []
    for i, g in enumerate(gdf):
        if verbose: t_start = time.time()
        gb = g.copy(deep=True)
        indb_invalid = ~gb.geometry.is_valid
        gb.geometry.loc[indb_invalid] = [
            shapely.wkb.loads(
                ogr.CreateGeometryFromWkb(g.to_wkb()).MakeValid().
                RemoveLowerDimensionSubGeoms().ExportToWkb())
            for g in gb.geometry.loc[indb_invalid]
        ]
        ret.append(gb)
        if verbose:
            print(
                f'GDF at index {i} corrected {sum(indb_invalid)} invalid geometries in {time.strftime("%H:%M:%S",time.gmtime(time.time()-t_start))}'
            )
    if unlist:
        ret = ret[0]
    return ret
Exemplo n.º 11
0
    def get_geometry(self, row):

        x, y = self.geo_vals(row)

        if self.type == 'point':
            geometry = ogr.Geometry(ogr.wkbPoint)
            geometry.SetPoint_2D(0, x, y)

        elif self.geo_col_names[0].lower() == 'geometry':
            geometry = ogr.CreateGeometryFromWkt(x)
        elif self.geo_col_names[0] == 'wkt':
            geometry = ogr.CreateGeometryFromWkt(x)
        elif self.geo_col_names[0] == 'wkb':
            geometry = ogr.CreateGeometryFromWkb(x)
        else:
            raise Exception("Didn't find geometry column")

        if geometry:
            if not geometry.TransformTo(self.srs):
                raise Exception("Failed to transform Geometry")
        else:
            raise Exception("Didn't get a geometry object for name {}".format(
                self.geo_col_names[0]))

        return geometry
Exemplo n.º 12
0
def convert_polygons_2_shp(shape_polygon, shape_file, template_file=None):

    template_handle = rasterio.open(template_file)
    metadata = template_handle.meta.copy()
    metadata.update(compress='lzw')

    driver = ogr.GetDriverByName('Esri Shapefile')
    ds = driver.CreateDataSource(shape_file)

    if shape_polygon.type == 'MultiPolygon':
        layer = ds.CreateLayer('', None, ogr.wkbMultiPolygon)
    elif shape_polygon.type == 'Polygon':
        layer = ds.CreateLayer('', None, ogr.wkbPolygon)
    else:
        raise IOError('Shape type not implemented yet')
    defn = layer.GetLayerDefn()
    feat = ogr.Feature(defn)

    # Make a geometry, from Shapely object
    geom = ogr.CreateGeometryFromWkb(shape_polygon.wkb)
    feat.SetGeometry(geom)

    layer.CreateFeature(feat)
    feat = geom = None  # destroy these

    # Save and close everything
    ds = layer = feat = geom = None
Exemplo n.º 13
0
def _postproc_feature(feature, kept_features, srs_transform=None, roi=None, allow_outlying=False, clip_outlying=False):
    if isinstance(feature["geometry"], dict):
        assert feature["geometry"]["type"] in ["Polygon", "MultiPolygon"], \
            f"unhandled raw geometry type: {feature['geometry']['type']}"
        if feature["geometry"]["type"] == "Polygon":
            coords = feature["geometry"]["coordinates"]
            assert isinstance(coords, list), "unexpected poly coords type"
            assert len(coords) == 1, "unexpected coords embedding; should be list-of-list-of-points w/ unique ring"
            assert all([isinstance(c, list) and len(c) == 2 for c in coords[0]]) and len(coords[0]) >= 4, \
                "unexpected poly coord format"
            poly = shapely.geometry.Polygon(coords[0])
            if srs_transform is not None:
                ogr_geometry = ogr.CreateGeometryFromWkb(poly.wkb)
                ogr_geometry.Transform(srs_transform)
                poly = shapely.wkt.loads(ogr_geometry.ExportToWkt())
            feature["geometry"] = poly
            feature["type"] = "Polygon"
        elif feature["geometry"]["type"] == "MultiPolygon":
            multipoly = shapely.geometry.shape(feature["geometry"])
            assert multipoly.is_valid, "found invalid input multipolygon"
            if srs_transform is not None:
                ogr_geometry = ogr.CreateGeometryFromWkb(multipoly.wkb)
                ogr_geometry.Transform(srs_transform)
                multipoly = shapely.wkt.loads(ogr_geometry.ExportToWkt())
            feature["geometry"] = multipoly
            feature["type"] = "MultiPolygon"
    assert isinstance(feature["geometry"], (shapely.geometry.polygon.Polygon,
                                            shapely.geometry.multipolygon.MultiPolygon))
    bounds = feature["geometry"].bounds
    feature["tl"] = bounds[0:2]
    feature["br"] = bounds[2:4]
    feature["clipped"] = False
    feature["centroid"] = feature["geometry"].centroid
    if roi is None:
        kept_features.append(feature)
    else:
        if (allow_outlying and roi.intersects(feature["geometry"])) or \
                (not allow_outlying and roi.contains(feature["geometry"])):
            if clip_outlying:
                if not roi.contains(feature["geometry"]):
                    feature["clipped"] = True
                    feature["geometry"] = roi.intersection(feature["geometry"])
                assert feature["geometry"].type in ["Polygon", "MultiPolygon"], \
                    f"unhandled intersection geometry type: {feature['geometry'].type}"
            feature["type"] = feature["geometry"].type
            kept_features.append(feature)
Exemplo n.º 14
0
 def readGeometry(self, geometry):
     self.list = []
     wkb = geometry.asWkb()
     geom = ogr.CreateGeometryFromWkb(wkb)
     geomCount = geom.GetPointCount()
     for pointIdx in range(geomCount):
         point = geom.GetPoint(pointIdx)
         self.addPoint(point)
Exemplo n.º 15
0
 def __call__(self, geom):
     try:
         geom2 = self.geom0.intersection(geom)
     except:
         import pdb; pdb.set_trace()
     if geom2 is None:
         import pdb; pdb.set_trace()
     geomx = ogr.CreateGeometryFromWkb(geom2.wkb)
     return geomx
Exemplo n.º 16
0
def max_range_linemerge_io(input_filename,
                           input_layername,
                           output_drivername,
                           output_filename,
                           output_layername,
                           merge_attribute,
                           max_range,
                           group_by: Union[str, Iterable] = None):
    # input
    in_ds = ogr.Open(input_filename)
    in_layer = in_ds.GetLayerByName(input_layername)
    geometry_groups = dict()
    value_groups = dict()
    if group_by:
        groups = tuple(distinct(in_layer, group_by))
    else:
        groups = tuple('DUMMY_GROUP_9876543210')
    print(f'groups: {groups}')
    for group in groups:
        geometry_groups[group] = []
        value_groups[group] = []
    for feat in in_layer:
        ogr_geom = feat.GetGeometryRef()
        wkb_geom = ogr_geom.ExportToWkb()
        feat_group = tuple(
            [feat[group_by_field] for group_by_field in group_by])
        geometry_groups[feat_group].append(wkb.loads(wkb_geom))
        value_groups[feat_group].append(feat[merge_attribute])

    # output
    out_driver = ogr.GetDriverByName(output_drivername)
    out_ds = out_driver.CreateDataSource(output_filename)
    srs = in_layer.GetSpatialRef()
    out_layer = out_ds.CreateLayer(output_layername,
                                   srs=srs,
                                   geom_type=ogr.wkbLineString)
    merge_field_defn = ogr.FieldDefn(merge_attribute, ogr.OFTReal)
    out_layer.CreateField(merge_field_defn)
    if group_by:
        for field in in_layer.schema:
            if field.name in group_by:
                out_layer.CreateField(field)

    # write results to output
    out_layer_defn = out_layer.GetLayerDefn()
    for group in groups:
        for geom, val in max_range_linemerge(geometry_groups[group],
                                             value_groups[group], max_range):
            out_feat = ogr.Feature(out_layer_defn)
            out_geom = ogr.CreateGeometryFromWkb(geom.wkb)
            out_feat.SetGeometry(out_geom)
            out_feat[merge_attribute] = val
            if group_by:
                for i, group_by_fieldname in enumerate(group_by):
                    out_feat[group_by_fieldname] = group[i]
            out_layer.CreateFeature(out_feat)
            out_feat = None
    def _create_base_map(self, ):
        '''
        Deal with different types way to define the AOI, if none is specified, then the image bound is used.
        '''
        if self.aoi is not None:
            if os.path.exists(self.aoi):
                try:
                    g = gdal.Open(self.aoi)
                    subprocess.call([
                        'gdaltindex', '-f', 'GeoJSON',
                        self.toa_dir + '/AOI.json', self.aoi
                    ])
                except:
                    try:
                        g = ogr.Open(self.aoi)
                    except:
                        raise IOError(
                            'AOI file cannot be opened by gdal, please check it or transform into format can be opened by gdal'
                        )
            else:
                try:
                    g = ogr.CreateGeometryFromJson(aoi)
                except:
                    try:
                        g = ogr.CreateGeometryFromGML(aoi)
                    except:
                        try:
                            g = ogr.CreateGeometryFromWkt(aoi)
                        except:
                            try:
                                g = ogr.CreateGeometryFromWkb(aoi)
                            except:
                                raise IOError(
                                    'The AOI has to be one of GeoJSON, GML, Wkt or Wkb.'
                                )
                gjson_str = '''{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":%s}]}''' % g.ExportToJson(
                )

                with open(self.toa_dir + '/AOI.json', 'wb') as f:
                    f.write(gjson_str)
        if not os.path.exists(self.toa_dir + '/AOI.json'):
            subprocess.call([
                'gdaltindex', '-f', 'GeoJSON', self.toa_dir + '/AOI.json',
                self.toa_bands[0]
            ])
            self.logger.warning(
                'AOI is not created and full band extend is used')
            self.aoi = self.toa_dir + '/AOI.json'
        else:
            self.aoi = self.toa_dir + '/AOI.json'

        if self.pixel_res is None:
            self.pixel_res = abs(
                gdal.Open(self.toa_bands[0]).GetGeoTransform()[1])

        self.psf_xstd = 260 / self.pixel_res
        self.psf_ystd = 340 / self.pixel_res
Exemplo n.º 18
0
def coords_to_geovector(coords, epsg, path_export):
    """
    Exports coordinates to a Point shapefile.

    Parameters
    ----------
    coords : list-like of list-likes
        List or tuple of (x, y) coordinates to export.
    epsg : int
        EPSG code of the coordinate reference system of the coordinates.
    path_export : str
        Path with .shp extension where the shapefile should be saved.

    Returns
    -------
    None.

    """
    # TODO: This should be replaced by a geodataframe creation, but no use cases
    # yet...


    all_coords = []
    for c in coords:
        pt = ogr.Geometry(type=ogr.wkbPoint)
        pt.AddPoint_2D(c[1], c[0])
        all_coords.append(pt)

    # Write the shapefile
    driver = ogr.GetDriverByName('ESRI Shapefile')
    datasource = driver.CreateDataSource(path_export)

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(epsg)

    layer = datasource.CreateLayer("Coords", srs, ogr.wkbPoint)
    defn = layer.GetLayerDefn()

    idField = ogr.FieldDefn('id', ogr.OFTInteger)
    layer.CreateField(idField)

    for i, p in enumerate(all_coords):

        # Create a new feature (attribute and geometry)
        feat = ogr.Feature(defn)
        feat.SetField('id', int(i))

        # Make a geometry
        geom = ogr.CreateGeometryFromWkb(p.ExportToWkb())
        feat.SetGeometry(geom)

        layer.CreateFeature(feat)
        feat = geom = None  # destroy these

    # Save and close everything
    datasource = layer = feat = geom = None
Exemplo n.º 19
0
    def process( self, args, writeback=False ):

        """
        get dataset handle and geolocation metadata 
        """

        # open image file
        image = self.openImageFile( args.image_pathname )
        data = image[ 'band' ].ReadAsArray()

        # create output shapefile
        centroids = self.createOutputFile( args.out_pathname, image )
        
        # generate threshold image
        idx = data > args.threshold
        mask = np.zeros_like( data )
        mask[ idx ] = 1

        # grab kernel and apply open operation
        kernel = cv2.getStructuringElement( cv2.MORPH_RECT, (5,5) )
        mask_open = cv2.morphologyEx( mask, cv2.MORPH_OPEN, kernel, iterations=args.iterations )

        # writeback
        if writeback:
            
            data[ np.where ( mask_open == 1 ) ] = 0
            plt.imshow( data, cmap='Greys' )
            plt.show()

        # label blob map
        labels = label( mask_open )
        for idx in range( 1, np.amax( labels ) + 1 ):

            # find location of blob with current label
            coords = np.where( labels == idx )
            if len ( coords[ 0 ] ) < self._maxBlobSize:
                
                # create shapely point object
                img_x = np.mean( coords[ 1 ] )
                img_y = np.mean( coords[ 0 ] )

                # compute geo coordinates
                geo_x = ( img_x * image[ 'transform' ][ 1 ] ) + image[ 'transform' ][ 0 ]
                geo_y = ( img_y * image[ 'transform' ][ 5 ] ) + image[ 'transform' ][ 3 ]

                # create new feature (attribute and geometry)
                feature = ogr.Feature( centroids[ 'defn' ] )
                feature.SetField( 'id', idx )

                # create geometry from shapely object
                geom = ogr.CreateGeometryFromWkb( Point( geo_x, geo_y ).to_wkb() )
                feature.SetGeometry(geom)

                centroids[ 'layer' ].CreateFeature( feature )

        return 
Exemplo n.º 20
0
def transform_geometry(geom, src_epsg=4326, dst_epsg=3395):
    """Transform a single wkb geometry
   :param geom: wkb geom
   :param src_epsg: EPSG code for the source geometry
   :param dst_epsg: EPSG code for the destination geometry
   """
    ogr_geom = ogr.CreateGeometryFromWkb(geom)
    ogr_transformation = create_transform(src_epsg, dst_epsg)
    ogr_geom.Transform(ogr_transformation)
    return ogr_geom.ExportToWkb()
Exemplo n.º 21
0
def fromGeometry(geometry):
    instance = QgsPolylineV2()
    wkb = geometry.asWkb()
    geom = ogr.CreateGeometryFromWkb(wkb)
    geomCount = geom.GetPointCount()
    for pointIdx in range(geomCount):
        point = geom.GetPoint(pointIdx)
        instance.addPoint(point)

    return instance
Exemplo n.º 22
0
def nodes_to_shapefile(nodes, dims, gt, epsg, outpath):

    # Create point objects to write to shapefile
    if 'id' not in nodes.keys():
        ids = list(range(0, len(nodes['idx'])))
    else:
        ids = nodes['id']

    all_nodes = []
    for node in nodes['idx']:
        pt = ogr.Geometry(type=ogr.wkbPoint)
        xy = np.unravel_index(node, dims)
        ll = gu.xy_to_coords(xy[1] + .5,
                             xy[0] + .5,
                             gt,
                             inputEPSG=epsg,
                             outputEPSG=epsg)[0]
        pt.AddPoint_2D(ll[1], ll[0])
        all_nodes.append(pt)

    # Write the shapefile
    driver = ogr.GetDriverByName('ESRI Shapefile')
    datasource = driver.CreateDataSource(outpath)

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(epsg)

    layer = datasource.CreateLayer("Nodes", srs, ogr.wkbPoint)
    defn = layer.GetLayerDefn()

    idField = ogr.FieldDefn('id', ogr.OFTInteger)
    connField = ogr.FieldDefn('conn', ogr.OFTString)

    layer.CreateField(idField)
    layer.CreateField(connField)

    for p, i in zip(all_nodes, ids):

        # Create a new feature (attribute and geometry)
        feat = ogr.Feature(defn)
        feat.SetField('id', int(i))

        fieldstr = str(nodes['conn'][nodes['id'].index(i)])
        fieldstr = fieldstr[1:-1]
        feat.SetField('conn', fieldstr)

        # Make a geometry
        geom = ogr.CreateGeometryFromWkb(p.ExportToWkb())
        feat.SetGeometry(geom)

        layer.CreateFeature(feat)
        feat = geom = None  # destroy these

    # Save and close everything
    datasource = layer = feat = geom = None
Exemplo n.º 23
0
def reproject_vector(in_vector, target_srs, out_fp='mem', driver="MEMORY"):
    '''
    Reproject a vector file from one projection to another
    :param in_vector: string or ogr.DataSource - path to source ds or ds
    :param target_srs: osr.SpatialReference - SRS to project to
    :param out_fp: optional, str - output filepath
    :param driver: ogr.Driver - driver to be used for output
    :return: ogr.DataSource if driver is MEMORY or None and file written to disk
    '''
    import ogr, osr

    # open file to reproject
    if isinstance(in_vector, str):
        in_vector = ogr.Open(in_vector)

    in_layer = in_vector.GetLayer()

    # set spatial reference and transformation
    in_srs = in_layer.GetSpatialRef()
    if isinstance(target_srs, int):
        target_srs = create_srs(target_srs)
    transform = osr.CoordinateTransformation(in_srs, target_srs)

    # create layer to copy information into
    driver = ogr.GetDriverByName(driver)
    out_ds = driver.CreateDataSource(out_fp)
    # checks which geometry type we're looking at automatically layer.GetLayerDefn().GetGeomType()
    out_layer = out_ds.CreateLayer('', target_srs,
                                   in_layer.GetLayerDefn().GetGeomType())
    in_feat = in_layer.GetFeature(0)
    # create same fields as in input
    for i in range(in_feat.GetFieldCount()):
        out_layer.CreateField(in_feat.GetFieldDefnRef(i))

    # apply transformation
    for i, in_feat in enumerate(in_layer):
        transformed = in_feat.GetGeometryRef()
        transformed.Transform(transform)

        geom = ogr.CreateGeometryFromWkb(transformed.ExportToWkb())
        defn = out_layer.GetLayerDefn()
        out_feat = ogr.Feature(defn)
        out_feat.SetGeometry(geom)
        # write information to fields
        for i in range(in_feat.GetFieldCount()):
            value = in_feat.GetField(i)
            out_feat.SetField(i, value)
        out_layer.CreateFeature(out_feat)
        out_feat = None

    if driver.GetDescription() == 'Memory':
        return out_ds

    del out_ds
Exemplo n.º 24
0
def write_vector(in_data, out_fp, driver="ESRI Shapefile", srs=None):
    import ogr

    out_ds = create_vector(out_fp, driver)
    if out_ds is None:
        print("Could not create file. Check if file exists and is in use.")
        return

    if srs is not None:
        if isinstance(srs, int):
            srs = create_srs(srs)
    else:
        srs = get_srs_vec(in_data)

    # create layer, layer definition
    if isinstance(in_data, ogr.DataSource):
        in_lyr = in_data.GetLayer()
    else:
        in_lyr = in_data
    geomType = in_lyr.GetGeomType()  # or in_data.GetLayerDefn().GetGeomType()
    out_lyr = out_ds.CreateLayer('', srs, geomType)
    # create/copy fiels from input
    in_feat = in_lyr.GetFeature(0)
    for i in range(in_feat.GetFieldCount()):
        out_lyr.CreateField(in_feat.GetFieldDefnRef(i))
    defn = out_lyr.GetLayerDefn()

    for i in range(in_lyr.GetFeatureCount()):
        in_feat = in_lyr.GetFeature(i)
        out_feat = ogr.Feature(defn)
        for i in range(in_feat.GetFieldCount()):
            value = in_feat.GetField(i)
            out_feat.SetField(i, value)
        geometry = in_feat.geometry().Clone()
        featureWkb = geometry.ExportToWkb()
        geometry = ogr.CreateGeometryFromWkb(featureWkb)
        out_feat.SetGeometry(geometry)
        out_lyr.CreateFeature(out_feat)

    #experimental change:
    #for feature in in_lyr:
    #    attribs = feature.items()
    #    out_feat = ogr.Feature(defn)
    #    for i, value in enumerate(attribs.values()):
    #        out_feat.SetField(i, value)
    #    geometry = in_feat.geometry().Clone()
    #    featureWkb = geometry.ExportToWkb()
    #    geometry = ogr.CreateGeometryFromWkb(featureWkb)
    #    out_feat.SetGeometry(geometry)
    #    out_lyr.CreateFeature(out_feat)

    out_feat = geometry = None  # destroy stuff
    out_ds = out_lyr = out_feat = geometry = None  # Save and close
Exemplo n.º 25
0
    def addPolygon(simplePolygon, dst_layer, index):
        featureDefn = dst_layer.GetLayerDefn()
        polygon = ogr.CreateGeometryFromWkb(simplePolygon)
        dst_feat = ogr.Feature(featureDefn)
        dst_feat.SetGeometry(polygon)

        geom = dst_feat.GetGeometryRef()
        dst_feat.SetField('id', index)
        dst_feat.SetField('area', geom.Area())

        dst_layer.CreateFeature(dst_feat)
        dst_layer.SyncToDisk()
Exemplo n.º 26
0
 def append_poly(feat, props, srs_transform=None):
     if feat.is_empty:
         return
     elif feat.type == "Polygon":
         if srs_transform is not None:
             ogr_geometry = ogr.CreateGeometryFromWkb(feat.wkb)
             ogr_geometry.Transform(srs_transform)
             feat = shapely.wkt.loads(ogr_geometry.ExportToWkt())
         output_features.append((geojson.Feature(geometry=feat, properties=props), feat))
     elif feat.type == "MultiPolygon" or feat.type == "GeometryCollection":
         for f in feat:
             append_poly(f, props)
Exemplo n.º 27
0
 def snapToDtm(self):
     player = self.edit3d_pointlayerbox.currentLayer()
     llayer = self.edit3d_linelayerbox.currentLayer()
     rlayer = self.edit3d_dtmlayerbox.currentLayer()
     llayer.startEditing()
     points = list(player.getFeatures())
     pointid = self.edit3d_currPointId.value()
     if points:
         point = points[pointid]
         pointGeom = point.geometry()
         if pointGeom.asMultiPoint():
             pointGeom = pointGeom.asMultiPoint()[0]
         else:
             pointGeom = pointGeom.asPoint()
         pid, feat = closestpoint(llayer, QgsGeometry.fromPoint(pointGeom))
         linegeom = feat.geometry().asWkb().data()
         olinegeom = ogr.CreateGeometryFromWkb(linegeom)
         dx = rlayer.rasterUnitsPerPixelX()
         dy = rlayer.rasterUnitsPerPixelY()
         xpos = pointGeom.x()
         ypos = pointGeom.y()
         # assume pixel = center
         xll = rlayer.extent().xMinimum() + 0.5 * dx
         yll = rlayer.extent().yMinimum() + 0.5 * dy
         xoffs = (pointGeom.x() - xll) % dx
         yoffs = (pointGeom.y() - yll) % dy
         dtm_val_ll = rlayer.dataProvider().identify(
             QgsPoint(xpos - dx / 2, ypos - dy / 2),
             QgsRaster.IdentifyFormatValue).results()[1]
         dtm_val_ur = rlayer.dataProvider().identify(
             QgsPoint(xpos + dx / 2, ypos + dy / 2),
             QgsRaster.IdentifyFormatValue).results()[1]
         dtm_val_lr = rlayer.dataProvider().identify(
             QgsPoint(xpos + dx / 2, ypos - dy / 2),
             QgsRaster.IdentifyFormatValue).results()[1]
         dtm_val_ul = rlayer.dataProvider().identify(
             QgsPoint(xpos - dx / 2, ypos + dy / 2),
             QgsRaster.IdentifyFormatValue).results()[1]
         a00 = dtm_val_ll
         a10 = dtm_val_lr - dtm_val_ll
         a01 = dtm_val_ul - dtm_val_ll
         a11 = dtm_val_ur + dtm_val_ll - (dtm_val_lr + dtm_val_ul)
         dtm_bilinear = a00 + a10 * xoffs + a01 * yoffs + a11 * xoffs * yoffs
         x, y = olinegeom.GetPoint_2D(pid)
         olinegeom.SetPoint(pid, x, y, dtm_bilinear)
         llayer.beginEditCommand("Snap point height to DTM")
         updatedGeom = QgsGeometry()
         updatedGeom.fromWkb(olinegeom.ExportToWkb())
         llayer.dataProvider().changeGeometryValues(
             {feat.id(): updatedGeom})
         llayer.endEditCommand()
         # refresh vertex editor
         self.showProblemPoint()
Exemplo n.º 28
0
    def linkshp_exp(self):
        try:
            # 입력파일 검증
            if os.path.exists(self._cc._LINK_SHP_FILE_PATH) == False:
                print(self._cc._LINK_SHP_FILE_PATH + ' 파일이 없습니다.')
                return -1

            self._logger.info("load link shp")
            link_shp = gpd.read_file(self._cc._LINK_SHP_FILE_PATH)
            link_shp.crs = {'init': 'epsg:4326'}

            self._logger.info("load link_dust info")
            dust_link = pd.read_csv(self._cc._DUST_LINK_TXT_FILE_PATH,
                                    sep=',',
                                    engine='python',
                                    usecols=['LINK_ID', 'CAI_VAL', 'CMID'
                                             ])  #'UV', 'PM10', 'PM2_5', 'CAI',

            self._logger.info("load all")

            link_shp['LINK_ID'] = link_shp['LINK_ID'].astype(np.int64)
            link_dust_line = pd.merge(link_shp,
                                      dust_link,
                                      how='inner',
                                      on='LINK_ID')

            self._logger.info("make link shape by ogr")
            drv = ogr.GetDriverByName('ESRI Shapefile')
            ds = drv.CreateDataSource(self._cc._DUST_LINK_LINE_FILE_PATH)
            lyr = ds.CreateLayer('myshp', geom_type=ogr.wkbLineString)

            fieldDef = ogr.FieldDefn('LINK_ID', ogr.OFTInteger64)
            lyr.CreateField(fieldDef)
            fieldDef = ogr.FieldDefn('CAI_VAL', ogr.OFTInteger)
            lyr.CreateField(fieldDef)
            fieldDef = ogr.FieldDefn('CMID', ogr.OFTString)
            lyr.CreateField(fieldDef)

            for index, item in link_dust_line.iterrows():
                feat = ogr.Feature(lyr.GetLayerDefn())
                feat.SetField('LINK_ID', item['LINK_ID'])
                feat.SetField('CAI_VAL', item['CAI_VAL'])
                feat.SetField('CMID', item['CMID'])
                #pt = ogr.CreateGeometryFromWkt(item['geometry'])
                line = ogr.CreateGeometryFromWkb(item['geometry'].to_wkb())
                feat.SetGeometry(line)
                lyr.CreateFeature(feat)

                feat = None

            self._logger.info("complete to make link shape by ogr")
        except KeyboardInterrupt:
            print('\n\rquit')
Exemplo n.º 29
0
def project(geom, from_epsg=900913, to_epsg=4326):
    # source: http://hackmap.blogspot.com/2008/03/ogr-python-projection.html
    to_srs = ogr.osr.SpatialReference()
    to_srs.ImportFromEPSG(to_epsg)

    from_srs = ogr.osr.SpatialReference()
    from_srs.ImportFromEPSG(from_epsg)

    ogr_geom = ogr.CreateGeometryFromWkb(geom.wkb)
    ogr_geom.AssignSpatialReference(from_srs)

    ogr_geom.TransformTo(to_srs)
    return loads(ogr_geom.ExportToWkb())
Exemplo n.º 30
0
def meshlines_to_shapefile(lines, EPSG, outpath, nameid=None):
    """
    Given meshlines (output by RivMesh), ouput a shapefile.
    """

    # Create line objects to write to shapefile
    all_lines = []
    for l in lines:
        line = ogr.Geometry(type=ogr.wkbLineString)
        line.AddPoint_2D(l[0][0], l[0][1])
        line.AddPoint_2D(l[1][0], l[1][1])
        all_lines.append(line)

    # Write the shapefile
    driver = ogr.GetDriverByName('ESRI Shapefile')
    datasource = driver.CreateDataSource(outpath)

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(EPSG)

    layer = datasource.CreateLayer("meshlines", srs, ogr.wkbLineString)
    defn = layer.GetLayerDefn()

    idField = ogr.FieldDefn('id', ogr.OFTInteger)
    layer.CreateField(idField)
    if nameid is not None:
        nameField = ogr.FieldDefn('River_Name', ogr.OFTString)
        layer.CreateField(nameField)

    for i, p in enumerate(all_lines):

        # Create a new feature (attribute and geometry)
        feat = ogr.Feature(defn)
        feat.SetField('id', int(i))

        # Set upstream and downstream node attributes
        if nameid is not None:
            fieldstr = str(nameid)
            feat.SetField('River_Name', fieldstr)

        # Make a geometry
        geom = ogr.CreateGeometryFromWkb(p.ExportToWkb())
        feat.SetGeometry(geom)

        layer.CreateFeature(feat)

        feat = geom = None  # destroy these

    # Save and close everything
    datasource = layer = feat = geom = None