def __init__(self, shapefile_path):

        #Load resources for coordinate transformations
        epsg27700 = SpatialReference()
        epsg27700.ImportFromEPSG(27700)

        epsg4326 = SpatialReference()
        epsg4326.ImportFromEPSG(4326)

        self.bng2latlon = CoordinateTransformation(epsg27700, epsg4326)
        self.latlon2bng = CoordinateTransformation(epsg4326, epsg27700)

        #Load shapefile
        r = sfl.Reader(shapefile_path)
        shapes = r.shapes()
        #calculate representive coordines for eah point by averaging the bounding box corners
        bboxes = [s.bbox for s in shapes]
        x_coords = [b[0] for b in bboxes] + [b[2] for b in bboxes]
        y_coords = [b[1] for b in bboxes] + [b[3] for b in bboxes]
        self.high_x = np.max(x_coords)
        self.high_y = np.max(y_coords)

        self.low_x = np.min(x_coords)
        self.low_y = np.min(y_coords)

        # print "Upper boundary:",self.high_x, self.high_y
        # print "Lower boundary:", self.low_x, self.low_y

        self.rep_coords = [((b[0] + b[2]) / 2.0, (b[1] + b[3]) / 2.0)
                           for b in bboxes]
        self.records = r.records()
        self.shapely_shapes = [Polygon(shape.points) for shape in shapes]
Пример #2
0
def toWGS84(x, y):
    epsg28992 = SpatialReference()
    epsg28992.ImportFromEPSG(28992)
    epsg28992.SetTOWGS84(565.237, 50.0087, 465.658, -0.406857, 0.350733,
                         -1.87035, 4.0812)
    epsg4326 = SpatialReference()
    epsg4326.ImportFromEPSG(4326)
    rd2latlon = CoordinateTransformation(epsg28992, epsg4326)
    latlon2rd = CoordinateTransformation(epsg4326, epsg28992)
    latlonz = rd2latlon.TransformPoint(x, y)
    return latlonz
Пример #3
0
def coord_conv(epsg_from,epsg_to,cord_xlong,cord_ylat):
    # 
    if epsg_from and epsg_to :

        epsgfrom = SpatialReference()
        epsgfrom.ImportFromEPSG(epsg_from)

        epsgto = SpatialReference()
        epsgto.ImportFromEPSG(epsg_to)

        #  Check for Projection exists
        if not epsgfrom.GetAttrValue("PROJCS|AUTHORITY", 1):
            cord_xlong=ddmmss_to_dd(cord_xlong)
            cord_ylat=ddmmss_to_dd(cord_ylat)

        psd2_IsProjected=False
        if epsgto.GetAttrValue("PROJCS|AUTHORITY", 1):
            psd2_IsProjected=True

        # Datum_epsg_from = epsgfrom.GetAttrValue("PROJCS|GEOGCS|AUTHORITY", 1)
        Datum_epsg_from = epsgfrom.GetAttrValue("GEOGCS|AUTHORITY", 1)
        Datum_epsg_to = epsgto.GetAttrValue("GEOGCS|AUTHORITY", 1)

        if int(Datum_epsg_from) == 4229:
            epsgfrom.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)

        if int(Datum_epsg_to) == 4229:
            epsgto.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)
        # ----------------------------
        if psd2_IsProjected:
            FromTo_psd = CoordinateTransformation(epsgfrom, epsgto)
            x_to,y_to,zcart_to = FromTo_psd.TransformPoint(cord_xlong, cord_ylat)
            x_to = format(round(x_to,2),'.2f')
            y_to = format(round(y_to,2),'.2f')
 
        else:
            x_to,y_to,zcart_to=None,None,None
    # -------- Case Projected To Get the datum from info of epsg2 and calculate lat/long------------------------
        epsgto.ImportFromEPSG(int(Datum_epsg_to))
        if int(Datum_epsg_to) == 4229:
            epsgto.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)
        FromTo_psd = CoordinateTransformation(epsgfrom, epsgto)
        long_to,lat_to,zcart2_to = FromTo_psd.TransformPoint(cord_xlong, cord_ylat)
        
        lat_to = ddmmss_to_dms(dd_to_ddmmss(lat_to)) + " N"
        long_to = ddmmss_to_dms(dd_to_ddmmss(long_to)) + " E"
        
    return (x_to,y_to,zcart_to,long_to,lat_to,zcart2_to)
Пример #4
0
def get_coordinate_transformation(from_wkt, to_wkt):
    '''
    Use GDAL to obtain a CoordinateTransformation object to transform coordinates between CRSs or None if no transformation required.
    @parameter from_wkt: WKT or "EPSG:nnnn" string from which to transform
    @parameter to_wkt: WKT or "EPSG:nnnn" string to which to transform
    '''
    # Assume native coordinates if no wkt given
    if not to_wkt or from_wkt == to_wkt:
        return None

    from_spatial_ref = get_spatial_ref_from_wkt(from_wkt)
    to_spatial_ref = get_spatial_ref_from_wkt(to_wkt)

    # This is probably redundant
    if not to_spatial_ref or from_spatial_ref.ExportToWkt(
    ) == to_spatial_ref.ExportToWkt():
        return None

    # Hack to make sure that traditional x-y coordinate order is always used
    if osgeo_version >= '3.':
        logger.debug(
            'Setting axis mapping strategy to XY  for GDAL 3.X  using OAMS_TRADITIONAL_GIS_ORDER'
        )
        from_spatial_ref.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
        to_spatial_ref.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)

    return CoordinateTransformation(from_spatial_ref, to_spatial_ref)
Пример #5
0
def reproj_convert_layer(geojson_path,
                         output_path,
                         file_format,
                         output_crs,
                         input_crs="epsg:4326"):
    layer_name = output_path.split('/')
    layer_name = layer_name[len(layer_name) - 1].split('.')[0]

    in_driver = GetDriverByName("GeoJSON")
    out_driver = GetDriverByName(file_format)

    inSpRef = SpatialReference()
    inSpRef.ImportFromEPSG(int(input_crs.split("epsg:")[1]))

    outSpRef = SpatialReference()
    ret_val = outSpRef.ImportFromProj4(output_crs)
    if not ret_val == 0:
        raise ValueError("Error when importing the output crs")

    coords_transform = CoordinateTransformation(inSpRef, outSpRef)

    f_in = in_driver.Open(geojson_path)
    input_layer = f_in.GetLayer()
    f_out = out_driver.CreateDataSource(output_path)
    output_layer = f_out.CreateLayer(layer_name, outSpRef)

    input_lyr_defn = input_layer.GetLayerDefn()
    for i in range(input_lyr_defn.GetFieldCount()):
        fieldDefn = input_lyr_defn.GetFieldDefn(i)
        output_layer.CreateField(fieldDefn)

    output_lyr_defn = output_layer.GetLayerDefn()

    for inFeature in input_layer:
        geom = inFeature.GetGeometryRef()
        outFeature = OgrFeature(output_lyr_defn)
        if geom:
            geom.Transform(coords_transform)
            outFeature.SetGeometry(geom)
        else:
            outFeature.SetGeometry(None)

        for i in range(output_lyr_defn.GetFieldCount()):
            outFeature.SetField(
                output_lyr_defn.GetFieldDefn(i).GetNameRef(),
                inFeature.GetField(i))
        output_layer.CreateFeature(outFeature)
        outFeature.Destroy()
        inFeature.Destroy()
    f_in.Destroy()
    f_out.Destroy()

    if "Shapefile" in file_format:
        outSpRef.MorphToESRI()
        with open(output_path.replace(".shp", ".prj"), 'w') as file_proj:
            file_proj.write(outSpRef.ExportToWkt())
        with open(output_path.replace(".shp", ".cpg"), "w") as encoding_file:
            encoding_file.write("ISO-8859-1")
    return 0
Пример #6
0
def convertCoordinateSystem(eo):
    # Define the TM central coordinate system (EPSG 5186)
    epsg5186 = SpatialReference()
    epsg5186.ImportFromEPSG(5186)

    # Define the wgs84 system (EPSG 4326)
    epsg4326 = SpatialReference()
    epsg4326.ImportFromEPSG(4326)

    tm2latlon = CoordinateTransformation(epsg5186, epsg4326)
    latlon2tm = CoordinateTransformation(epsg4326, epsg5186)

    # Check the transformation for a point close to the centre of the projected grid
    xy = latlon2tm.TransformPoint(float(eo[0]), float(eo[1]))   # The order: Lat, Lon
    eo[0:2] = xy[0:2]

    return eo
Пример #7
0
def wgs84To28992(lo, la):
    # Define the Rijksdriehoek projection system (EPSG 28992)
    epsg28992 = SpatialReference()
    epsg28992.ImportFromEPSG(28992)

    # correct the towgs84
    epsg28992.SetTOWGS84(565.237, 50.0087, 465.658, -0.406857, 0.350733,
                         -1.87035, 4.0812)

    # Define the wgs84 system (EPSG 4326)
    epsg4326 = SpatialReference()
    epsg4326.ImportFromEPSG(4326)

    rd2latlon = CoordinateTransformation(epsg28992, epsg4326)
    latlon2rd = CoordinateTransformation(epsg4326, epsg28992)

    # Check the transformation for a point close to the centre of the projected grid
    xy = latlon2rd.TransformPoint(lo, la)
    return ([xy[0], xy[1]])
Пример #8
0
def latLonToUtmPoint(lon, lat, targetEpsg):

    targetSrs = osr.SpatialReference()
    targetSrs.ImportFromEPSG(int(targetEpsg))

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

    coordTrans = CoordinateTransformation(srs, targetSrs)
    utmLon, utmLat = coordTrans.TransformPoint(lon, lat)[0:2]

    return utmLon, utmLat
Пример #9
0
def convert_ogr_to_geojson(file_path, file_format):

    regex_field_name = re.compile("[^a-zA-Z0-9_-ëêàáâãæêéèñòóô]+")

    in_driver = GetDriverByName(file_format)
    out_driver = GetDriverByName('MEMORY')

    f_in = in_driver.Open(file_path)
    input_layer = f_in.GetLayer()

    outSpRef = SpatialReference()
    outSpRef.ImportFromEPSG(4326)
    coords_transform = CoordinateTransformation(input_layer.GetSpatialRef(),
                                                outSpRef)

    f_out = out_driver.CreateDataSource('')
    output_layer = f_out.CreateLayer('', outSpRef)

    input_lyr_defn = input_layer.GetLayerDefn()
    for i in range(input_lyr_defn.GetFieldCount()):
        fieldDefn = input_lyr_defn.GetFieldDefn(i)
        fieldDefn.SetName(regex_field_name.sub('_', fieldDefn.GetNameRef()))
        output_layer.CreateField(fieldDefn)

    output_lyr_defn = output_layer.GetLayerDefn()
    nb_field = output_lyr_defn.GetFieldCount()
    field_names = [
        output_lyr_defn.GetFieldDefn(i).GetNameRef() for i in range(nb_field)
    ]

    res = []
    for inFeature in input_layer:
        geom = inFeature.GetGeometryRef()
        outFeature = OgrFeature(output_lyr_defn)
        # Don't try to transform empty geometry :
        if geom:
            geom.Transform(coords_transform)
            outFeature.SetGeometry(geom)
        else:
            outFeature.SetGeometry(None)
        outFeature.SetFID(inFeature.GetFID())
        for i in range(output_lyr_defn.GetFieldCount()):
            outFeature.SetField(field_names[i], inFeature.GetField(i))
        res.append(outFeature.ExportToJson())
        outFeature.Destroy()
        inFeature.Destroy()

    f_in.Destroy()
    f_out.Destroy()

    return ''.join([
        '''{"type":"FeatureCollection","features":[''', ','.join(res), ''']}'''
    ]).encode()
Пример #10
0
    def __init__(self, filename=FILENAME):
        print(f"[INFO] Reading geofile from '{filename}'")
        self.dataset = gdal.Open(filename, gdal.GA_ReadOnly)
        self.geotransform = self.dataset.GetGeoTransform()
        self.band = self.dataset.GetRasterBand(1)

        srcRef = SpatialReference()
        srcRef.SetWellKnownGeogCS("WGS84")
        dstRef = SpatialReference(self.dataset.GetProjection())
        self.ct = CoordinateTransformation(srcRef, dstRef)

        gdalVersion = osgeo.gdal.__version__
        print(f"[INFO] Using gdal ver. {gdalVersion}")
        self.gdalMajorVer = int(gdalVersion[:gdalVersion.find('.')])
Пример #11
0
def geographic2plane(eo, epsg):
    # Define the Plane Coordinate System (EPSG 5186)
    plane = SpatialReference()
    plane.ImportFromEPSG(epsg)

    # Define the wgs84 system (EPSG 4326)
    geographic = SpatialReference()
    geographic.ImportFromEPSG(4326)

    coord_transformation = CoordinateTransformation(geographic, plane)

    # Check the transformation for a point close to the centre of the projected grid
    xy = coord_transformation.TransformPoint(float(eo[0]), float(
        eo[1]))  # The order: Lon, Lat
    return xy[0:2]
Пример #12
0
def tmcentral2latlon(eo):
    # Define the TM central coordinate system (EPSG 5186)
    epsg5186 = SpatialReference()
    epsg5186.ImportFromEPSG(5186)

    # Define the wgs84 system (EPSG 4326)
    epsg4326 = SpatialReference()
    epsg4326.ImportFromEPSG(4326)

    tm2latlon = CoordinateTransformation(epsg5186, epsg4326)

    # Check the transformation for a point close to the centre of the projected grid
    lonlat = tm2latlon.TransformPoint(float(eo[0]), float(eo[1]))  # The order: x, y
    eo[0:2] = lonlat[0:2]

    return eo
Пример #13
0
def plane2geographic(xy, epsg=32610):
    # Define the Plane Coordinate System (e.g. 5186)
    plane = SpatialReference()
    plane.ImportFromEPSG(epsg)

    # Define the wgs84 system (EPSG 4326)
    geographic = SpatialReference()
    geographic.ImportFromEPSG(4326)

    coord_transformation = CoordinateTransformation(plane, geographic)

    # Check the transformation for a point close to the centre of the projected grid
    latlon = coord_transformation.TransformPoint(float(xy[0]), float(
        xy[1]))  # The order: x, y

    return latlon[:2]
Пример #14
0
    def initcc(self):
        """
        initialize coordinate conversion
        """
        if not hasattr(self, 'rd2latlon'):
            from osgeo.osr import SpatialReference, CoordinateTransformation

            # Define the Rijksdriehoek projection system (EPSG 28992)
            epsg28992 = SpatialReference()
            epsg28992.ImportFromEPSG(28992)
            # correct the towgs84
            epsg28992.SetTOWGS84(565.237, 50.0087, 465.658, -0.406857,
                                 0.350733, -1.87035, 4.0812)

            # Define the wgs84 system (EPSG 4326)
            epsg4326 = SpatialReference()
            epsg4326.ImportFromEPSG(4326)
            self.rd2latlon = CoordinateTransformation(epsg28992, epsg4326)
Пример #15
0
def get_coordinate_transformation(from_wkt, to_wkt):
    '''
    Use GDAL to obtain a CoordinateTransformation object to transform coordinates between CRSs or None if no transformation required.
    @parameter from_wkt: WKT or "EPSG:nnnn" string from which to transform
    @parameter to_wkt: WKT or "EPSG:nnnn" string to which to transform
    '''
    # Assume native coordinates if no wkt given
    if from_wkt == to_wkt:
        return None

    from_spatial_ref = get_spatial_ref_from_wkt(from_wkt)
    to_spatial_ref = get_spatial_ref_from_wkt(to_wkt)

    # This is probably redundant
    if from_spatial_ref.ExportToWkt() == to_spatial_ref.ExportToWkt():
        return None

    return CoordinateTransformation(from_spatial_ref, to_spatial_ref)
Пример #16
0
def coord_conv(epsg_from, epsg_to, cord_xlong, cord_ylat):
    #
    if epsg_from and epsg_to:

        epsgfrom = SpatialReference()
        epsgfrom.ImportFromEPSG(epsg_from)

        if epsg_from != 4326:
            epsgfrom.SetTOWGS84(-121.8, 98.1, -10.7, 0, 0, 0.554, -0.2263)

        epsgto = SpatialReference()
        epsgto.ImportFromEPSG(epsg_to)

        if epsg_to != 4326:
            epsgto.SetTOWGS84(-121.8, 98.1, -10.7, 0, 0, 0.554, -0.2263)
    # ----------------------------
    FromTo_psd = CoordinateTransformation(epsgfrom, epsgto)
    xlong, ylat, zcart = FromTo_psd.TransformPoint(cord_xlong, cord_ylat)

    # print(xlong,ylat,zcart)
    return (xlong, ylat, zcart)
Пример #17
0
def crs_transform(x, y, from_crs, to_crs):
    """Transform coordinate values between two coordinate systems

    Transform coordinate values between two coordinate systems. The shape
    of the input arrays are preserved.

    :param x:
        First coordinate array
    :type: numpy.ndarray
    :param y:
        Second coordinate array
    :type: numpy.ndarray
    :param from_crs:
        Source coordinates reference frame
    :type from_crs: osgeo.osr.SpatialReference
    :param to_crs:
        Transformed coordinates reference frame
    :type to_crs: osgeo.osr.SpatialReference
    :returns:
        (xp, yp), the transformed coordinates
    :rtype: (numpy.ndarray, numpy.ndarray)
    """

    xarr = np.array(x)
    yarr = np.array(y)

    if len(xarr) == 0 and len(yarr) == 0:
        return np.array([x, y])

    ct = CoordinateTransformation(from_crs, to_crs)

    xrv = xarr.ravel()
    yrv = yarr.ravel()
    points = np.stack([xrv, yrv, np.zeros_like(xrv)]).T
    result = np.array(ct.TransformPoints(points))
    xp = result[:, 0].reshape(xarr.shape)
    yp = result[:, 1].reshape(yarr.shape)
    return xp, yp
Пример #18
0
def geographic2plane(eo, epsg=5186):
    # Define the Plane Coordinate System (e.g. 5186)
    plane = SpatialReference()
    plane.ImportFromEPSG(epsg)

    # Define the wgs84 system (EPSG 4326)
    geographic = SpatialReference()
    geographic.ImportFromEPSG(4326)

    coord_transformation = CoordinateTransformation(geographic, plane)

    # Check the transformation for a point close to the centre of the projected grid
    if int(osgeo.__version__[0]) >= 3:  # version 3.x
        # Transform(y,x) will return x,y (Easting, Northing)
        yx = coord_transformation.TransformPoint(float(eo[1]), float(
            eo[0]))  # The order: Lat, Lon
        eo[0:2] = yx[0:2][::-1]
    else:  # version 2.x
        # Transform(x,y) will return x,y (Easting, Northing)
        xy = coord_transformation.TransformPoint(float(eo[0]), float(
            eo[1]))  # The order: Lon, Lat
        eo[0:2] = xy[0:2]

    return eo
Пример #19
0
def write_foglio(foglio,
                 destination,
                 point_borders=False,
                 format_name='ESRI Shapefile'):

    cassini_soldener = ''
    #Imposto alcune variabile a seconda del codice_comune:
    #ATTENZIONE: Prima di modificare qui lo SRID controllare che su Postgres le tavole siano impostate adeguatamente nella tavola geometry_columns!!!
    #ATTENZIONE: se nella definizione della cassini_soldener si inseriscono dei valori fissi di x_0 e y_0 ricordarsi di definire successivamente la local_cassini_soldener in maniera adeguata, cioe' togliendo il riferimento al vettore shift_cassini prima definito. In pratica aggiungere il codice_comune nell'array del primo "if" (verso rigo 103...)
    if foglio['CODICE COMUNE'] == 'G087':
        cassini_soldener = '+proj=cass +lat_0=45.007336 +lon_0=7.53725 +x_0=%f +y_0=%f +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'B305':
        cassini_soldener = '+proj=cass +lat_0=45.067618 +lon_0=7.436827 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I785':
        cassini_soldener = '+proj=cass +lat_0=37.267029 +lon_0=14.692473 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'G535':
        cassini_soldener = '+proj=cass +lat_0=44.759075 +lon_0=9.917936 +x_0=-15.5 +y_0=10.5 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'G476':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'L380':
        cassini_soldener = '+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +towgs84=-104.1,-49.1,-9.9,0.971,-2.917,0.714,-11.68 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I258':
        cassini_soldener = '+proj=cass +lat_0=45.099116 +lon_0=7.356182 +x_0=-1.5 +y_0=0.5 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'C261':
        cassini_soldener = '+proj=cass +lat_0=45.31413 +lon_0=9.502994 +x_0=1 +y_0=1 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'C722':
        cassini_soldener = "+proj=cass +lat_0=45.235812 +lon_0=7.602194 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs +wktext"
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'A484':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'G793':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I089':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I143':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I307':
        cassini_soldener = '+proj=cass +lat_0=40.535328 +lon_0=15.324016 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'G226':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'B266':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'B868':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'F618':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'F625':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'H683':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I410':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'I451':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'C370':
        cassini_soldener = '+proj=cass +lat_0=45.558239 +lon_0=10.76357 +x_0=+0.45 +y_0=-1.90 +ellps=intl +units=m +no_defs'
        t_srs = '4326'
    elif foglio['CODICE COMUNE'] == 'D292':
        cassini_soldener = '+proj=cass +lat_0=40.283555 +lon_0=15.483897 +x_0=8.9958 +y_0=-8.3549 +ellps=bessel +towgs84=668.8,146.4,506.5,5.187,-2.54,5.256,0 +units=m +no_defs'
    target_srs = SpatialReference()
    try:
        target_srs.ImportFromEPSG(int(t_srs))
    except TypeError:
        raise
        target_srs.ImportFromProj4(t_srs)

    shifts = ((0., 0.), (0., 0.))
    shifts = comuni_shift.get(foglio['CODICE COMUNE'], shifts)
    shifts = comuni_shift.get(
        (foglio['CODICE COMUNE'], foglio['NUMERO FOGLIO']), shifts)

    shift_cassini, shift_gauss_boaga = shifts
    ##### Parte eventualmente da MODIFICARE:
    if foglio['CODICE COMUNE'] in [
            'G535', 'I258', 'L380', 'G476', 'C261', 'A484', 'B266', 'B868',
            'F618', 'F625', 'G226', 'G793', 'I307', 'I410', 'I451', 'D292',
            'I143', 'I089', 'H683', 'C722', 'B305', 'I785', 'C370'
    ]:
        local_cassini_soldener = cassini_soldener
    else:
        local_cassini_soldener = cassini_soldener % (-shift_cassini[0],
                                                     -shift_cassini[1])

    source_srs = SpatialReference()
    source_srs.ImportFromProj4(local_cassini_soldener)

    trasformation = CoordinateTransformation(source_srs, target_srs)

    f_comune = FieldDefn('COMUNE', OFTString)
    f_comune.SetWidth(4)
    f_foglio = FieldDefn('FOGLIO', OFTString)
    f_foglio.SetWidth(11)
    f_tipo = FieldDefn('tipo', OFTString)
    f_tipo.SetWidth(11)
    f_part = FieldDefn('PARTICELLA', OFTString)
    f_part.SetWidth(8)
    f_numero = FieldDefn('NUMERO', OFTString)
    f_part.SetWidth(8)
    f_dimensione = FieldDefn('DIMENSIONE', OFTInteger)
    f_area = FieldDefn('AREA', OFTInteger)
    f_angolo = FieldDefn('ANGOLO', OFTReal)
    f_pos_x = FieldDefn('POSIZIONEX', OFTReal)
    f_pos_y = FieldDefn('POSIZIONEY', OFTReal)
    f_interno_x = FieldDefn('P_INTERNOX', OFTReal)
    f_interno_y = FieldDefn('P_INTERNOY', OFTReal)
    f_simbolo = FieldDefn('SIMBOLO', OFTInteger)
    f_etichetta = FieldDefn('etichetta', OFTString)
    f_etichetta.SetWidth(32)
    f_testo = FieldDefn('TESTO', OFTString)
    f_testo.SetWidth(256)

    create_options = []
    if format_name == 'PostgreSQL':
        #Per passare i parametri del driver nella forma "parametro=valore". Sfortunatamente NON POSSO PASSARE "-APPEND"!!!!
        #vedi anche: http://www.gdal.org/gdal_tutorial.html
        papszOptions = ['OVERWRITE=yes']
    elif format_name == 'SQLite':  #IN SVILUPPO!
        #per maggiori info vedere: http://www.gdal.org/drv_sqlite.html
        create_options = [
            'SPATIALITE=YES', 'INIT_WITH_EPSG=YES',
            'OGR_SQLITE_SYNCHRONOUS=OFF', 'OVERWRITE=yes'
        ]
        #l'opzione Overwrite sul DB non funziona: ho messo una IF oltre
        papszOptions = ['FORMAT=SPATIALITE', 'OVERWRITE=yes']  #default
    else:
        papszOptions = []

    if (format_name == 'SQLite') and (os.path.exists(destination)):
        ds = GetDriverByName(format_name).Open(destination, update=1)
    #Pensavo in questo modo di ovviare all'errore che mi restituisce lo script nel caso di DB:
    #ERROR 1: PostgreSQL driver doesn't currently support database creation. Please create database with the `createdb' command.
    #ma non ho risolto niente... Invece aggiungendo "PG:" il plugin genera le tabelle!
    elif (format_name == 'PostgreSQL'):
        #    ds = GetDriverByName(format_name).Open(destination)
        #destination = "PG:%s" % (destination)
        ds = GetDriverByName(format_name).CreateDataSource(
            destination, options=create_options)
    else:
        ds = GetDriverByName(format_name).CreateDataSource(
            destination, options=create_options)
    #per evitare sovrascritture aggiungo anche l'allegato
    pedice = "%s_%s_%s_%s" % (foglio['CODICE COMUNE'], foglio['NUMERO FOGLIO'],
                              foglio['CODICE ALLEGATO'],
                              foglio['CODICE SVILUPPO'])

    #PLUGIN QGIS:
    #Decodifico alcuni campi in modo tale che vengano riconosciuti corretti anche dalle librerie interne di QGis:
    comune_decode = remove_accents(foglio['CODICE COMUNE'])
    #oppure potrebbe essere:
    #comune_decode = foglio['CODICE COMUNE'].encode('utf-8')
    codice_foglioXX = foglio['CODICE FOGLIO'][5:
                                              9]  #cosi' dovrebbe essere "0036"
    foglio_intero = int(codice_foglioXX.lstrip('0'))

    # tipo BORDO
    #bordi = ds.CreateLayer('CATASTO_BORDI', target_srs, wkbPolygon)
    nome_layer_not_utf = "CATASTO_BORDI_%s" % (pedice)
    nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
    bordi = ds.CreateLayer(nome_layer, target_srs, wkbPolygon25D, papszOptions)

    bordi.CreateField(f_comune)
    bordi.CreateField(f_foglio)
    bordi.CreateField(f_tipo)
    bordi.CreateField(f_part)
    bordi.CreateField(f_dimensione)
    bordi.CreateField(f_angolo)
    bordi.CreateField(f_pos_x)
    bordi.CreateField(f_pos_y)
    bordi.CreateField(f_interno_x)
    bordi.CreateField(f_interno_y)
    bordi.CreateField(f_area)
    bordi.CreateField(f_etichetta)

    for oggetto in foglio['oggetti']['BORDO']:
        poly = Geometry(wkbPolygon)
        tabisole = map(int, oggetto['TABISOLE'])

        # contorno esterno
        vertici_contorno = int(oggetto['NUMEROVERTICI']) - sum(tabisole)
        ring = Geometry(wkbLinearRing)
        for vertice in range(vertici_contorno):
            x, y = map(float, oggetto['VERTICI'][vertice])
            if True:
                x, y = trasformation.TransformPoint(x, y)[:2]
            ring.AddPoint(x + shift_gauss_boaga[0], y + shift_gauss_boaga[1])
        ring.CloseRings()
        poly.AddGeometry(ring)

        # isole
        for isola in range(int(oggetto['NUMEROISOLE'])):
            ring = Geometry(wkbLinearRing)
            for vertice in range(vertice + 1, vertice + 1 + tabisole[isola]):
                x, y = map(float, oggetto['VERTICI'][vertice])
                if True:
                    x, y = trasformation.TransformPoint(x, y)[:2]
                ring.AddPoint(x + shift_gauss_boaga[0],
                              y + shift_gauss_boaga[1])
            ring.CloseRings()
            poly.AddGeometry(ring)

        etichetta = oggetto['CODICE IDENTIFICATIVO']
        if oggetto['CODICE IDENTIFICATIVO'][-1] == '+':
            etichetta = ''

        feat = Feature(bordi.GetLayerDefn())
        feat.SetField('COMUNE',
                      comune_decode)  #plugin in QGis necessita di decodifica
        #codice_foglioXX = foglio['CODICE FOGLIO'][5:9] #cosi' dovrebbe essere "0036"
        #feat.SetField('FOGLIO', codice_foglioXX.lstrip('0'))
        feat.SetField('FOGLIO',
                      foglio_intero)  #plugin in QGis necessita di decodifica
        feat.SetField('tipo', oggetto['tipo'])
        #feat.SetField('PARTICELLA', oggetto['CODICE IDENTIFICATIVO']) #voglio togliere il "+"
        feat.SetField('PARTICELLA',
                      oggetto['CODICE IDENTIFICATIVO'].rstrip('+'))
        feat.SetField('DIMENSIONE', int(oggetto['DIMENSIONE']))
        feat.SetField('ANGOLO', float(oggetto['ANGOLO']))
        pos_x, pos_y = map(float,
                           (oggetto['POSIZIONEX'], oggetto['POSIZIONEY']))
        interno_x, interno_y = map(
            float, (oggetto['PUNTOINTERNOX'], oggetto['PUNTOINTERNOY']))
        if True:
            pos_x, pos_y = trasformation.TransformPoint(pos_x, pos_y)[:2]
            interno_x, interno_y = trasformation.TransformPoint(
                interno_x, interno_y)[:2]
        feat.SetField('POSIZIONEX', pos_x + shift_gauss_boaga[0])
        feat.SetField('POSIZIONEY', pos_y + shift_gauss_boaga[1])
        feat.SetField('P_INTERNOX', interno_x + shift_gauss_boaga[0])
        feat.SetField('P_INTERNOY', interno_y + shift_gauss_boaga[1])
        feat.SetField('AREA', oggetto.get('AREA', -1))
        feat.SetField('etichetta', etichetta.encode('utf-8'))
        feat.SetGeometry(poly)
        bordi.CreateFeature(feat)
        feat.Destroy()

    if point_borders:
        # tipo BORDO_PUNTO
        #bordi = ds.CreateLayer('CATASTO_PARTICELLE', target_srs, wkbPoint)
        #nome_layer = "CATASTO_PARTICELLE_%s" % (pedice)
        nome_layer_not_utf = "CATASTO_PARTICELLE_%s" % (pedice)
        nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
        bordi = ds.CreateLayer(nome_layer, target_srs, wkbPoint, papszOptions)

        bordi.CreateField(f_comune)
        bordi.CreateField(f_foglio)
        bordi.CreateField(f_tipo)
        bordi.CreateField(f_part)
        bordi.CreateField(f_dimensione)
        bordi.CreateField(f_angolo)
        bordi.CreateField(f_area)
        bordi.CreateField(f_etichetta)

        for oggetto in foglio['oggetti']['BORDO']:
            etichetta = oggetto['CODICE IDENTIFICATIVO']
            if oggetto['CODICE IDENTIFICATIVO'][-1] == '+':
                etichetta = ''

            feat = Feature(bordi.GetLayerDefn())
            #feat.SetField('COMUNE', foglio['CODICE COMUNE'])
            feat.SetField(
                'COMUNE',
                comune_decode)  #plugin in QGis necessita di decodifica
            #feat.SetField('FOGLIO', foglio['CODICE FOGLIO'])
            feat.SetField(
                'FOGLIO',
                foglio_intero)  #plugin in QGis necessita di decodifica
            feat.SetField('tipo', oggetto['tipo'])
            feat.SetField('PARTICELLA', oggetto['CODICE IDENTIFICATIVO'])
            feat.SetField('DIMENSIONE', int(oggetto['DIMENSIONE']))
            feat.SetField('ANGOLO', float(oggetto['ANGOLO']))
            pos_x, pos_y = map(
                float, (oggetto['PUNTOINTERNOX'], oggetto['PUNTOINTERNOY']))
            if True:
                pos_x, pos_y = trasformation.TransformPoint(pos_x, pos_y)[:2]
            feat.SetField('AREA', oggetto.get('AREA', -1))
            feat.SetField('etichetta', etichetta.encode('utf-8'))
            pt = Geometry(wkbPoint)
            pt.SetPoint_2D(0, pos_x + shift_gauss_boaga[0],
                           pos_y + shift_gauss_boaga[1])
            feat.SetGeometry(pt)
            bordi.CreateFeature(feat)
            feat.Destroy()

    # tipo TESTO
    #testi = ds.CreateLayer('CATASTO_TESTI', target_srs, wkbPoint)
    #nome_layer = "CATASTO_TESTI_%s" % (pedice)
    nome_layer_not_utf = "CATASTO_TESTI_%s" % (pedice)
    nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
    testi = ds.CreateLayer(nome_layer, target_srs, wkbPoint, papszOptions)

    testi.CreateField(f_comune)
    testi.CreateField(f_foglio)
    testi.CreateField(f_testo)
    testi.CreateField(f_dimensione)
    testi.CreateField(f_angolo)
    testi.CreateField(f_etichetta)

    for oggetto in foglio['oggetti']['TESTO']:
        x, y = map(float, (oggetto['POSIZIONEX'], oggetto['POSIZIONEY']))
        if True:
            x, y = trasformation.TransformPoint(x, y)[:2]
        # FIXME: many texts are useless, prun them from etichetta
        etichetta = remove_accents(oggetto['TESTO'])

        feat = Feature(testi.GetLayerDefn())
        #feat.SetField('COMUNE', foglio['CODICE COMUNE'])
        feat.SetField('COMUNE',
                      comune_decode)  #plugin in QGis necessita di decodifica
        #feat.SetField('FOGLIO', foglio['CODICE FOGLIO'])
        feat.SetField('FOGLIO',
                      foglio_intero)  #plugin in QGis necessita di decodifica
        #feat.SetField('TESTO', oggetto['TESTO'])
        feat.SetField('TESTO', etichetta)
        feat.SetField('DIMENSIONE', int(oggetto['DIMENSIONE']))
        feat.SetField('ANGOLO', float(oggetto['ANGOLO']))
        feat.SetField('etichetta', etichetta.encode('utf-8'))
        pt = Geometry(wkbPoint)
        pt.SetPoint_2D(0, x + shift_gauss_boaga[0], y + shift_gauss_boaga[1])
        feat.SetGeometry(pt)
        testi.CreateFeature(feat)

    # tipo SIMBOLO
    #simboli = ds.CreateLayer('CATASTO_SIMBOLI', target_srs, wkbPoint)
    #nome_layer = "CATASTO_SIMBOLI_%s" % (pedice)
    nome_layer_not_utf = "CATASTO_SIMBOLI_%s" % (pedice)
    nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
    simboli = ds.CreateLayer(nome_layer, target_srs, wkbPoint, papszOptions)

    simboli.CreateField(f_comune)
    simboli.CreateField(f_foglio)
    simboli.CreateField(f_simbolo)
    simboli.CreateField(f_angolo)

    for oggetto in foglio['oggetti']['SIMBOLO']:
        x, y = map(float, (oggetto['POSIZIONEX'], oggetto['POSIZIONEY']))
        if True:
            x, y = trasformation.TransformPoint(x, y)[:2]

        feat = Feature(simboli.GetLayerDefn())
        #feat.SetField('COMUNE', foglio['CODICE COMUNE'])
        feat.SetField('COMUNE',
                      comune_decode)  #plugin in QGis necessita di decodifica
        #feat.SetField('FOGLIO', foglio['CODICE FOGLIO'])
        feat.SetField('FOGLIO',
                      foglio_intero)  #plugin in QGis necessita di decodifica
        feat.SetField('SIMBOLO', oggetto['CODICE SIMBOLO'])
        feat.SetField('ANGOLO', float(oggetto['ANGOLO']))
        pt = Geometry(wkbPoint)
        pt.SetPoint_2D(0, x + shift_gauss_boaga[0], y + shift_gauss_boaga[1])
        feat.SetGeometry(pt)
        simboli.CreateFeature(feat)

    # tipo FIDUCIALE
    #fiduciali = ds.CreateLayer('CATASTO_FIDUCIALI', target_srs, wkbPoint)
    #nome_layer = "CATASTO_FIDUCIALI_%s" % (pedice)
    nome_layer_not_utf = "CATASTO_FIDUCIALI_%s" % (pedice)
    nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
    fiduciali = ds.CreateLayer(nome_layer, target_srs, wkbPoint, papszOptions)

    fiduciali.CreateField(f_comune)
    fiduciali.CreateField(f_foglio)
    fiduciali.CreateField(f_numero)
    fiduciali.CreateField(f_simbolo)
    fiduciali.CreateField(f_pos_x)
    fiduciali.CreateField(f_pos_y)
    fiduciali.CreateField(f_etichetta)

    print 'corrections', shift_cassini, shift_gauss_boaga
    for oggetto in foglio['oggetti']['FIDUCIALE']:
        x, y = map(float, (oggetto['POSIZIONEX'], oggetto['POSIZIONEY']))
        pos_x, pos_y = map(float, (oggetto['PUNTORAPPRESENTAZIONEX'],
                                   oggetto['PUNTORAPPRESENTAZIONEY']))
        if True:
            x, y = trasformation.TransformPoint(x, y)[:2]
            pos_x, pos_y = trasformation.TransformPoint(pos_x, pos_y)[:2]
        etichetta = 'PF%02d/%s%s/%s' % (int(oggetto['NUMERO IDENTIFICATIVO']),
                                        foglio['CODICE NUMERO FOGLIO'][1:],
                                        foglio['CODICE ALLEGATO'],
                                        foglio['CODICE COMUNE'])

        feat = Feature(fiduciali.GetLayerDefn())
        #feat.SetField('COMUNE', foglio['CODICE COMUNE'])
        feat.SetField('COMUNE',
                      comune_decode)  #plugin in QGis necessita di decodifica
        #feat.SetField('FOGLIO', foglio['CODICE FOGLIO'])
        feat.SetField('FOGLIO',
                      foglio_intero)  #plugin in QGis necessita di decodifica
        feat.SetField('NUMERO', oggetto['NUMERO IDENTIFICATIVO'])
        feat.SetField('SIMBOLO', oggetto['CODICE SIMBOLO'])
        feat.SetField('POSIZIONEX', pos_x + shift_gauss_boaga[0])
        feat.SetField('POSIZIONEY', pos_y + shift_gauss_boaga[1])
        feat.SetField('etichetta', etichetta.encode('utf-8'))
        pt = Geometry(wkbPoint)
        pt.SetPoint_2D(0, x + shift_gauss_boaga[0], y + shift_gauss_boaga[1])
        feat.SetGeometry(pt)
        fiduciali.CreateFeature(feat)

        print etichetta, oggetto['CODICE SIMBOLO'], \
            float(oggetto['POSIZIONEX']) + shift_cassini[0], float(oggetto['POSIZIONEY']) + shift_cassini[1], \
            x + shift_gauss_boaga[0], y + shift_gauss_boaga[1]

    # tipo LINEA
    #linee = ds.CreateLayer('CATASTO_LINEE', target_srs, wkbLineString)
    #nome_layer = "CATASTO_LINEE_%s" % (pedice)
    nome_layer_not_utf = "CATASTO_LINEE_%s" % (pedice)
    nome_layer = nome_layer_not_utf.encode('utf-8')  #serve per plugin QGis
    linee = ds.CreateLayer(nome_layer, target_srs, wkbLineString25D,
                           papszOptions)

    linee.CreateField(f_comune)
    linee.CreateField(f_foglio)
    linee.CreateField(f_simbolo)

    for oggetto in foglio['oggetti']['LINEA']:
        # contorno esterno
        vertici = int(oggetto['NUMEROVERTICI'])
        linea = Geometry(wkbLineString)
        for vertice in range(vertici):
            x, y = map(float, oggetto['VERTICI'][vertice])
            if True:
                x, y = trasformation.TransformPoint(x, y)[:2]
            linea.AddPoint(x + shift_gauss_boaga[0], y + shift_gauss_boaga[1])

        feat = Feature(linee.GetLayerDefn())
        #feat.SetField('COMUNE', foglio['CODICE COMUNE'])
        feat.SetField('COMUNE',
                      comune_decode)  #plugin in QGis necessita di decodifica
        #feat.SetField('FOGLIO', foglio['CODICE FOGLIO'])
        feat.SetField('FOGLIO',
                      foglio_intero)  #plugin in QGis necessita di decodifica
        feat.SetField('SIMBOLO', oggetto['CODICE TIPO DI TRATTO'])
        feat.SetGeometry(linea)
        linee.CreateFeature(feat)
        feat.Destroy()

    ds.Destroy()
Пример #20
0
def map_dataframe(dataframe,
                  variable_string1,
                  max_color,
                  mid_color,
                  popup_string1,
                  save_string,
                  variable_string2=None,
                  popup_string2=None,
                  variable_string3=None,
                  popup_string3=None,
                  variable_string4=None,
                  popup_string4=None):
    """
    Map dataframe function
    Maps the dataframe given as input to a folium map
    Saves the map in the assigned location
    :return: None
    """
    with Session() as s:
        ## Load the location data in a pandas dataframe
        locationdata = parquet.readLocatie(s).toPandas()

        ## Used to convert coordinates from RDnew to WGS84
        epsg28992 = SpatialReference()
        epsg28992.ImportFromEPSG(28992)

        epsg28992.SetTOWGS84(565.237, 50.0087, 465.658, -0.406857, 0.350733,
                             -1.87035, 4.0812)

        epsg4326 = SpatialReference()
        epsg4326.ImportFromEPSG(4326)

        rd2latlon = CoordinateTransformation(epsg28992, epsg4326)

        ## Set center of map to center of Utrecht and create map
        SF_COORDINATES = (52.092876, 5.104480)
        map = folium.Map(location=SF_COORDINATES, zoom_start=14)

        ## Set a marker for each location in the dataframe
        for each in locationdata.iterrows():
            for line in dataframe.collect():
                if each[1]['MeetpuntRichtingCode'] == line['UniekeMeetpuntRichtingCode'] + '-1' or \
                                each[1]['MeetpuntRichtingCode'] == line['UniekeMeetpuntRichtingCode']:

                    ## Select all coordinates which are not 'null'
                    if each[1]['XRD'] != '':
                        ## Convert coordinates
                        X, Y, Z = rd2latlon.TransformPoint(
                            int(each[1]['XRD']), int(each[1]['YRD']))
                        ## Create a marker for each location
                        if variable_string4 != None:
                            if line[variable_string1] > max_color or line[variable_string2] > max_color or \
                                            line[variable_string3] > max_color or line[variable_string4] > max_color:
                                color = 'red'
                            elif line[variable_string1] > mid_color or line[variable_string2] > mid_color or \
                                            line[variable_string3] > mid_color or line[variable_string4] > mid_color:
                                color = 'orange'
                            else:
                                color = 'green'
                            folium.Marker(
                                location=[Y, X],
                                popup=(folium.Popup(
                                    popup_string1 + ': ' +
                                    str(int(line[variable_string1])) + ', ' +
                                    popup_string2 + ': ' +
                                    str(int(line[variable_string2])) + ', ' +
                                    popup_string3 + ': ' +
                                    str(int(line[variable_string3])) + ', ' +
                                    popup_string4 + ': ' +
                                    str(int(line[variable_string4])),
                                    max_width=200)),
                                icon=folium.Icon(color=color,
                                                 icon='road')).add_to(map)
                        elif variable_string3 != None:
                            if line[variable_string1] > max_color or line[variable_string3] > max_color or \
                                            line[variable_string4] > max_color:
                                color = 'red'
                            elif line[variable_string1] > mid_color or line[
                                    variable_string2] > mid_color or line[
                                        variable_string3] > mid_color:
                                color = 'orange'
                            else:
                                color = 'green'
                            folium.Marker(
                                location=[Y, X],
                                popup=(folium.Popup(
                                    popup_string1 + ': ' +
                                    str(int(line[variable_string1])) + ', ' +
                                    popup_string2 + ': ' +
                                    str(int(line[variable_string2])) + ', ' +
                                    popup_string3 + ': ' +
                                    str(int(line[variable_string3])),
                                    max_width=200)),
                                icon=folium.Icon(color=color,
                                                 icon='road')).add_to(map)
                        elif variable_string2 != None:
                            if line[variable_string1] > max_color or line[
                                    variable_string2] > max_color:
                                color = 'red'
                            elif line[variable_string1] > mid_color or line[
                                    variable_string2] > mid_color:
                                color = 'orange'
                            else:
                                color = 'green'
                            folium.Marker(
                                location=[Y, X],
                                popup=(folium.Popup(
                                    popup_string1 + ': ' +
                                    str(int(line[variable_string1])) + ', ' +
                                    popup_string2 + ': ' +
                                    str(int(line[variable_string2])),
                                    max_width=200)),
                                icon=folium.Icon(color=color,
                                                 icon='road')).add_to(map)
                        else:
                            if line[variable_string1] > max_color:
                                color = 'red'
                            elif line[variable_string1] > mid_color:
                                color = 'orange'
                            else:
                                color = 'green'
                            folium.Marker(
                                location=[Y, X],
                                popup=(popup_string1 + ': ' +
                                       str(int(line[variable_string1]))),
                                icon=folium.Icon(color=color,
                                                 icon='road')).add_to(map)

        ## Save the map to a .html file
        map.save(save_string)
Пример #21
0
epsgPurpleBelt.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)

epsgRedBelt = SpatialReference()
epsgRedBelt.ImportFromEPSG(22992)
epsgRedBelt.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)

# correct the towgs84
# Egy 1907 7-par
# Define the wgs84 system (EPSG 4326)
epsgEgypt1907 = SpatialReference()
epsgEgypt1907.ImportFromEPSG(4229)
epsgEgypt1907.SetTOWGS84(-121.8,98.1,-10.7,0,0,0.554,-0.2263)

epsgWgs84 = SpatialReference()
epsgWgs84.ImportFromEPSG(4326)


Red_WGS_latlon = CoordinateTransformation(epsgRedBelt, epsgWgs84)
WGS_Red_latlon2rd = CoordinateTransformation(epsgWgs84, epsgRedBelt)
pur_to_red = CoordinateTransformation(epsgPurpleBelt, epsgRedBelt)  
pur_to_wgs = CoordinateTransformation(epsgPurpleBelt, epsgWgs84)  

# Check the transformation for a point close to the centre of the projected grid
# Red_WGS_latz = Red_WGS_latlon.TransformPoint(615000.0, 810000.0)
# print(Red_WGS_latz) # (5.387203018813555, 52.002375635973344, 43.614926571026444)
# WGS_longLat_Red = WGS_Red_latlon2rd.TransformPoint(31, 30)
# print(WGS_longLat_Red) # (5.387203018813555, 52.002375635973344, 43.614926571026444)

Red_xy = pur_to_wgs.TransformPoint(670934.110, 305758.950)
print(Red_xy) # (5.387203018813555, 52.002375635973344, 43.614926571026444)
Пример #22
0
def reproj_convert_layer(geojson_path, output_path, file_format, output_crs):
    """
    Concert GeoJSON to GML or Shapefile and write it to disk.

    Convert a GeoJSON FeatureCollection to GML or ESRI Shapefile format and
    reproject the geometries if needed (used when the user requests an export).

    Parameters
    ----------
    geojson_path: str
        Path of the input GeoJSON FeatureCollection to be converted.

    output_path: str
        Path for the resulting Shapefile/GML (should be in a directory
        created by tempfile.TemporaryDirectory).

    file_format: str
        The format of the expected result ('ESRI Shapefile' or 'GML' is expected).

    output_crs: str
        The output srs to use (in proj4 string format).

    Returns
    -------
    result_code: int
        Should return 0 if everything went fine..
    """
    ## TODO : Use VectorTranslate to make the conversion?
    input_crs = "epsg:4326"
    layer_name = output_path.split('/')
    layer_name = layer_name[len(layer_name) - 1].split('.')[0]

    in_driver = GetDriverByName("GeoJSON")
    out_driver = GetDriverByName(file_format)

    inSpRef = SpatialReference()
    inSpRef.ImportFromEPSG(int(input_crs.split("epsg:")[1]))

    outSpRef = SpatialReference()
    ret_val = outSpRef.ImportFromProj4(output_crs)
    if not ret_val == 0:
        raise ValueError("Error when importing the output crs")

    coords_transform = CoordinateTransformation(inSpRef, outSpRef)

    f_in = in_driver.Open(geojson_path)
    input_layer = f_in.GetLayer()
    f_out = out_driver.CreateDataSource(output_path)
    output_layer = f_out.CreateLayer(layer_name, outSpRef)

    input_lyr_defn = input_layer.GetLayerDefn()
    for i in range(input_lyr_defn.GetFieldCount()):
        fieldDefn = input_lyr_defn.GetFieldDefn(i)
        output_layer.CreateField(fieldDefn)

    output_lyr_defn = output_layer.GetLayerDefn()

    for inFeature in input_layer:
        geom = inFeature.GetGeometryRef()
        outFeature = OgrFeature(output_lyr_defn)
        if geom:
            geom.Transform(coords_transform)
            outFeature.SetGeometry(geom)
        else:
            outFeature.SetGeometry(None)

        for i in range(output_lyr_defn.GetFieldCount()):
            outFeature.SetField(
                output_lyr_defn.GetFieldDefn(i).GetNameRef(),
                inFeature.GetField(i))
        output_layer.CreateFeature(outFeature)
        outFeature.Destroy()
        inFeature.Destroy()
    f_in.Destroy()
    f_out.Destroy()

    if "Shapefile" in file_format:
        outSpRef.MorphToESRI()
        with open(output_path.replace(".shp", ".prj"), 'w') as file_proj:
            file_proj.write(outSpRef.ExportToWkt())
        with open(output_path.replace(".shp", ".cpg"), "w") as encoding_file:
            encoding_file.write("ISO-8859-1")
    return 0
Пример #23
0
 def transform(self, target_proj):
     ct = CoordinateTransformation(self.proj, target_proj)
     geoms = [g.Clone() for g in self]
     [g.Transform(ct) for g in geoms]
     return VectorLayer(geoms, proj=target_proj, index=self.index)
Пример #24
0
    def set_netcdf_metadata_attributes(
            self, to_crs='EPSG:4326', do_stats=False):
        '''
        Function to set all NetCDF metadata attributes using self.METADATA_MAPPING to map from NetCDF ACDD global attribute name to metadata path (e.g. xpath)
        Parameter:
            to_crs: EPSG or WKT for spatial metadata
            do_stats: Boolean flag indicating whether minmax stats should be determined (slow)
        '''
        assert self.METADATA_MAPPING, 'No metadata mapping defined'
        assert self._netcdf_dataset, 'NetCDF output dataset not defined.'
#        assert self._metadata_dict, 'No metadata acquired'

        # Set geospatial attributes
        try:
            grid_mapping = [variable.grid_mapping for variable in self._netcdf_dataset.variables.values(
            ) if hasattr(variable, 'grid_mapping')][0]
        except:
            logger.error(
                'Unable to determine grid_mapping for spatial reference')
            raise

        crs = self._netcdf_dataset.variables[grid_mapping]

        spatial_ref = crs.spatial_ref
        geoTransform = [float(string)
                        for string in crs.GeoTransform.strip().split(' ')]
        xpixels, ypixels = (
            dimension.size for dimension in self._netcdf_dataset.dimensions.values())
        dimension_names = (
            dimension.name for dimension in self._netcdf_dataset.dimensions.values())

        # Create nested list of bounding box corner coordinates
        bbox_corners = [[geoTransform[0] + (x_pixel_offset * geoTransform[1]) + (y_pixel_offset * geoTransform[2]),
                         geoTransform[3] + (x_pixel_offset * geoTransform[4]) + (y_pixel_offset * geoTransform[5])]
                        for x_pixel_offset in [0, xpixels]
                        for y_pixel_offset in [0, ypixels]]

        if to_crs:  # Coordinate transformation required
            from_spatial_ref = SpatialReference()
            from_spatial_ref.ImportFromWkt(spatial_ref)

            to_spatial_ref = SpatialReference()
            # Check for EPSG then Well Known Text
            epsg_match = re.match('^EPSG:(\d+)$', to_crs)
            if epsg_match:
                to_spatial_ref.ImportFromEPSG(int(epsg_match.group(1)))
            else:  # Assume valid WKT definition
                to_spatial_ref.ImportFromWkt(to_crs)

            coord_trans = CoordinateTransformation(
                from_spatial_ref, to_spatial_ref)

            extents = np.array(
                [coord[0:2] for coord in coord_trans.TransformPoints(bbox_corners)])
            spatial_ref = to_spatial_ref.ExportToWkt()

            centre_pixel_coords = [coord[0:2] for coord in coord_trans.TransformPoints(
                [[geoTransform[0] + (x_pixel_offset * geoTransform[1]) + (y_pixel_offset * geoTransform[2]),
                  geoTransform[3] + (x_pixel_offset * geoTransform[4]) + (y_pixel_offset * geoTransform[5])]
                 for x_pixel_offset in [xpixels // 2, xpixels // 2 + 1]
                 for y_pixel_offset in [ypixels // 2, ypixels // 2 + 1]]
            )
            ]

            # Use Pythagoras to compute centre pixel size in new coordinates
            # (never mind the angles)
            yres = pow(pow(centre_pixel_coords[1][0] - centre_pixel_coords[0][0], 2) + pow(
                centre_pixel_coords[1][1] - centre_pixel_coords[0][1], 2), 0.5)
            xres = pow(pow(centre_pixel_coords[2][0] - centre_pixel_coords[0][0], 2) + pow(
                centre_pixel_coords[2][1] - centre_pixel_coords[0][1], 2), 0.5)

            # TODO: Make this more robust - could pull single unit from WKT
            if to_spatial_ref.IsGeographic():
                xunits, yunits = ('degrees_east', 'degrees_north')
            elif to_spatial_ref.IsProjected():
                xunits, yunits = ('m', 'm')
            else:
                xunits, yunits = ('unknown', 'unknown')

        else:  # Use native coordinates
            extents = np.array(bbox_corners)
            xres = round(geoTransform[1], Geophys2NetCDF.DECIMAL_PLACES)
            yres = round(geoTransform[5], Geophys2NetCDF.DECIMAL_PLACES)
            xunits, yunits = (self._netcdf_dataset.variables[
                              dimension_name].units for dimension_name in dimension_names)

        xmin = np.min(extents[:, 0])
        ymin = np.min(extents[:, 1])
        xmax = np.max(extents[:, 0])
        ymax = np.max(extents[:, 1])

        attribute_dict = dict(zip(['geospatial_lon_min', 'geospatial_lat_min', 'geospatial_lon_max', 'geospatial_lat_max'],
                                  [xmin, ymin, xmax, ymax]
                                  )
                              )
        attribute_dict['geospatial_lon_resolution'] = xres
        attribute_dict['geospatial_lat_resolution'] = yres
        attribute_dict['geospatial_lon_units'] = xunits
        attribute_dict['geospatial_lat_units'] = yunits

        try:
            convex_hull = [coordinate[0:2] for coordinate in coord_trans.TransformPoints(
                netcdf2convex_hull(self.netcdf_dataset, 2000000000))]  # Process dataset in pieces <= 2GB in size
        except:
            logger.info('Unable to compute convex hull. Using rectangular bounding box instead.')
            convex_hull = [coordinate[0:2] for coordinate in coord_trans.TransformPoints(bbox_corners + [bbox_corners[0]])]

        attribute_dict['geospatial_bounds'] = 'POLYGON((' + ', '.join([' '.join(
            ['%.4f' % ordinate for ordinate in coordinates]) for coordinates in convex_hull]) + '))'

        attribute_dict['geospatial_bounds_crs'] = spatial_ref

        for key, value in attribute_dict.items():
            setattr(self._netcdf_dataset, key, value)

        # Set attributes defined in self.METADATA_MAPPING
        # Scan list in reverse to give priority to earlier entries
        #TODO: Improve this coding - it's a bit crap
        keys_read = []
        for key, metadata_path in self.METADATA_MAPPING:
            # Skip any keys already read
            if key in keys_read:
                continue

            value = self.get_metadata(metadata_path)
            if value is not None:
                logger.debug('Setting %s to %s', key, value)
                # TODO: Check whether hierarchical metadata required
                setattr(self._netcdf_dataset, key, value)
                keys_read.append(key)
            else:
                logger.warning(
                    'WARNING: Metadata path %s not found', metadata_path)

        unread_keys = sorted(
            list(set([item[0] for item in self.METADATA_MAPPING]) - set(keys_read)))
        if unread_keys:
            logger.warning(
                'WARNING: No value found for metadata attribute(s) %s' % ', '.join(unread_keys))

        # Ensure only one DOI is stored - could be multiple, comma-separated
        # entries
        if hasattr(self._netcdf_dataset, 'doi'):
            url_list = [url.strip()
                        for url in self._netcdf_dataset.doi.split(',')]
            doi_list = [url for url in url_list if url.startswith(
                'http://dx.doi.org/')]
            if len(url_list) > 1:  # If more than one URL in list
                try:  # Give preference to proper DOI URL
                    url = doi_list[0]  # Use first (preferably only) DOI URL
                except:
                    url = url_list[0]  # Just use first URL if no DOI found
                url = url.replace('&amp;', '&')
                self._netcdf_dataset.doi = url

        # Set metadata_link to NCI metadata URL
        self._netcdf_dataset.metadata_link = 'https://pid.nci.org.au/dataset/%s' % self.uuid

        self._netcdf_dataset.Conventions = 'CF-1.6, ACDD-1.3'

        if do_stats:
            datastats = DataStats(netcdf_dataset=self.netcdf_dataset,
                                  netcdf_path=None, max_bytes=2000000000)  # 2GB pieces
            datastats.data_variable.actual_range = np.array(
                [datastats.value('min'), datastats.value('max')], dtype='float32')

        # Remove old fields - remove this later
        if hasattr(self._netcdf_dataset, 'id'):
            del self._netcdf_dataset.id
        if hasattr(self._netcdf_dataset, 'ga_uuid'):
            del self._netcdf_dataset.ga_uuid
        if hasattr(self._netcdf_dataset, 'keywords_vocabulary'):
            del self._netcdf_dataset.keywords_vocabulary
Пример #25
0
def process_gml(input_filename,
                outName,
                field_mapping,
                icon_mapping,
                epsg=None):

    if epsg is not None:
        # Define the  projection system (EPSG 28992) sourcedata
        source_epsg = SpatialReference()
        source_epsg.ImportFromEPSG(epsg)
    else:
        source_epsg = SpatialReference()
        source_epsg.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
        source_epsg.ImportFromEPSG(4326)

    # Define the wgs84 system (EPSG 4326)
    epsg4326 = SpatialReference()
    epsg4326.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER)
    epsg4326.ImportFromEPSG(4326)

    outDriver = ogr.GetDriverByName('GPX')
    co_opts = ['GPX_USE_EXTENSIONS=yes', 'GPX_EXTENSIONS_NS="{opencpn}"']
    outDataSource = outDriver.CreateDataSource(outName, options=co_opts)
    outLayer = outDataSource.CreateLayer('waypoints',
                                         epsg4326,
                                         geom_type=ogr.wkbPoint)
    featureDefn = outLayer.GetLayerDefn()

    reader = ogr.Open(input_filename, update=0)

    print("GetLayerCount() = %d\n", reader.GetLayerCount())
    for iLayer in range(reader.GetLayerCount()):
        poLayer = reader.GetLayer(iLayer)
        # poLayer.SetSpatialFilter(epsg28992)

        line = "Layer %d: %s" % (iLayer + 1, poLayer.GetLayerDefn().GetName())
        print(line)

        line = "geocount %d: %s" % (iLayer + 1,
                                    poLayer.GetLayerDefn().GetGeomFieldCount())

        nGeomFieldCount = poLayer.GetLayerDefn().GetGeomFieldCount()
        if nGeomFieldCount > 0:
            line = line + " ("
            for iGeom in range(nGeomFieldCount):
                if iGeom > 0:
                    line = line + ", "
                poGFldDefn = poLayer.GetLayerDefn().GetGeomFieldDefn(iGeom)
                line = line + "%s" % ogr.GeometryTypeToName(
                    poGFldDefn.GetType())
            line = line + ")"

        if poLayer.GetLayerDefn().GetGeomType() != ogr.wkbUnknown:
            line = line + " (%s)" % ogr.GeometryTypeToName(
                poLayer.GetLayerDefn().GetGeomType())

        print(line)

        poFeature = poLayer.GetNextFeature()
        while poFeature is not None:
            poDefn = poFeature.GetDefnRef()
            print("OGRFeature(%s):%ld" %
                  (poDefn.GetName(), poFeature.GetFID()))
            outFeature = ogr.Feature(featureDefn)
            outFeature.SetFrom(poFeature)
            poDstGeometry: ogr.Geometry = outFeature.GetGeometryRef()
            if poDstGeometry is not None:
                print('geometry input:       ', poDstGeometry)
                srsReference: SpatialReference = poDstGeometry.GetSpatialReference(
                )
                if srsReference is None:
                    srsReference = source_epsg
                    print('no SpatialReference, using default')
                poCT = CoordinateTransformation(srsReference, epsg4326)
                if poCT is not None:
                    eErr = poDstGeometry.Transform(poCT)
                    print('geometry converted:   ', poDstGeometry)
                    if eErr != 0:
                        print(
                            "Failed to reproject feature %d (geometry probably out of source or destination SRS)."
                            % poFeature.GetFID())

            description = []

            for iField in range(poDefn.GetFieldCount()):
                poFDefn = poDefn.GetFieldDefn(iField)
                line = "  %s (%s) = " % (poFDefn.GetNameRef(),
                                         ogr.GetFieldTypeName(
                                             poFDefn.GetType()))
                if poFeature.IsFieldSet(
                        iField) and poFDefn.GetNameRef() in field_mapping:
                    try:
                        line = line + "%s" % (
                            poFeature.GetFieldAsString(iField))
                        map = field_mapping[poFDefn.GetNameRef()]
                        value = poFeature.GetFieldAsString(iField)

                        if map['isDescription']:
                            if len(
                                    value
                            ) > 0 and value != 'Niet toegewezen' and value != '#':
                                description.append(
                                    '%s : %s' %
                                    (map['dst'],
                                     poFeature.GetFieldAsString(iField)))
                        else:
                            outFeature.SetField(
                                map['dst'], poFeature.GetFieldAsString(iField))

                        # for the icons do a lookup
                        if map['dst'] == 'sym':
                            if value in icon_mapping:
                                outFeature.SetField(map['dst'],
                                                    str(icon_mapping[value]))
                            else:
                                outFeature.SetField(
                                    map['dst'],
                                    poFeature.GetFieldAsString(iField))
                                if value not in missing:
                                    missing.append(value)

                    except UnicodeEncodeError:
                        # For Python3 on non-UTF8 strings
                        print('pynonUT', errno)
                        # exit()
                        line = line + "%s" % (
                            poFeature.GetFieldAsBinary(iField))
                else:
                    line = line + "(null)"
                if debugging:
                    print(line)

            if len(description) > 0:
                outFeature.SetField('desc', '\n'.join(description))
            outLayer.CreateFeature(outFeature)
            # dereference the feature
            outFeature = None

            poFeature = poLayer.GetNextFeature()
    reader.Destroy()
    outDataSource.Destroy()
Пример #26
0
ur_x, ur_y = (107000, 510000)
width = 50
dist = 50
###Translate translation to WGS84(EPSG4326)
# Define the Rijksdriehoek projection system (EPSG 28992)
epsg28992 = SpatialReference()
epsg28992.ImportFromEPSG(28992)

# correct the towgs84
epsg28992.SetTOWGS84(565.237, 50.0087, 465.658, -0.406857, 0.350733, -1.87035,
                     4.0812)

# Define the wgs84 system (EPSG 4326)
epsg4326 = SpatialReference()
epsg4326.ImportFromEPSG(4326)
rd2latlon = CoordinateTransformation(epsg28992, epsg4326)
latlon2rd = CoordinateTransformation(epsg4326, epsg28992)
latlon1 = rd2latlon.TransformPoint(lon_1, lat_1)
latlon2 = rd2latlon.TransformPoint(lon_2, lat_2)
rasterLL = rd2latlon.TransformPoint(origin_x, origin_y)
rasterUR = rd2latlon.TransformPoint(ur_x, ur_y)

###Large data formats (DIS, LPF, RCH, VDF)
###Bas
bas_file = (r'modflowtest.bas')
bas_data = pd.read_csv(bas_file, skiprows=3, delim_whitespace=True)
bas_arr = bas_data.to_numpy()
basl1 = bas_arr[0:107, :]
basl2 = bas_arr[108:216, :]
basl3 = bas_arr[217:325, :]
basl4 = bas_arr[326:434, :]
Пример #27
0
data = []
shapes = []

for i in range(ds.GetLayerCount()):
    lyr = ds.GetLayer(i)
    lyr.ResetReading()
    layerName = lyr.GetName()

    #print lyr.GetGeometryColumn()

    fromRef = lyr.GetSpatialRef()
    if fromRef is None:
        sys.exit(4)

    coordTransform = CoordinateTransformation(fromRef, toRef)
    numFeatures = lyr.GetFeatureCount()
    feat = lyr.GetNextFeature()
    if numFeatures == 1:
        row = {}
        geom = feat.GetGeometryRef()
        if ext == '.gpx':
            geom = geom.ConvexHull()
        fid = feat.GetFID()

        if geom.IsValid() and geom.IsSimple():
            geom.Transform(coordTransform)

            row['fid'] = feat.GetFID()
            #geom = geom.ConvexHull()
            row['shape'] = geom.ExportToWkt()
Пример #28
0
def convert_ogr_to_geojson(file_path, file_format):
    """
    Convert a layer (in a format supported by ogr) to a GeoJSON layer.
    Used in fallback, if the conversion failed with 'VectorTranslate'.

    Parameters
    ----------
    file_path: str
        Path of the input file.
    file_format: str
        Format of the input layer

    Returns
    -------
    raw_geojson: bytes
        The resulting GeoJSON FeatureCollection.
    """
    regex_field_name = re.compile("[^a-zA-Z0-9_-ëêàáâãæêéèñòóô]+")

    in_driver = GetDriverByName(file_format)
    out_driver = GetDriverByName('MEMORY')

    f_in = in_driver.Open(file_path)
    input_layer = f_in.GetLayer()

    outSpRef = SpatialReference()
    outSpRef.ImportFromEPSG(4326)
    coords_transform = CoordinateTransformation(input_layer.GetSpatialRef(),
                                                outSpRef)

    f_out = out_driver.CreateDataSource('')
    output_layer = f_out.CreateLayer('', outSpRef)

    input_lyr_defn = input_layer.GetLayerDefn()
    for i in range(input_lyr_defn.GetFieldCount()):
        fieldDefn = input_lyr_defn.GetFieldDefn(i)
        fieldDefn.SetName(regex_field_name.sub('_', fieldDefn.GetNameRef()))
        output_layer.CreateField(fieldDefn)

    output_lyr_defn = output_layer.GetLayerDefn()
    nb_field = output_lyr_defn.GetFieldCount()
    field_names = [
        output_lyr_defn.GetFieldDefn(i).GetNameRef() for i in range(nb_field)
    ]

    res = []
    for inFeature in input_layer:
        geom = inFeature.GetGeometryRef()
        outFeature = OgrFeature(output_lyr_defn)
        # Don't try to transform empty geometry :
        if geom:
            geom.Transform(coords_transform)
            outFeature.SetGeometry(geom)
        else:
            outFeature.SetGeometry(None)
        outFeature.SetFID(inFeature.GetFID())
        for i in range(output_lyr_defn.GetFieldCount()):
            outFeature.SetField(field_names[i], inFeature.GetField(i))
        res.append(outFeature.ExportToJson())
        outFeature.Destroy()
        inFeature.Destroy()

    f_in.Destroy()
    f_out.Destroy()

    return ''.join([
        '''{"type":"FeatureCollection","features":[''', ','.join(res), ''']}'''
    ]).encode()
Пример #29
0
def to_geometry(shp, copy=False, proj=None):
    """Convert shp to a ogr.Geometry.

    Parameters
    ----------
    shp: ogr.Geometry, ogr.Feature, or shapely.BaseGeometry
        The shape you want to convert

    copy: boolean (default=False)
        Return a copy of the shape instead of a reference

    proj: str or osr.SpatialReference (default=None)
        The projection of the shape to define (if the shape is
        not projection aware), or transform to (if projection aware).
        If a string is provided, it assumes that it is in PROJ4.

    Returns
    -------
    ogr.Geometry"""

    target_proj = None
    source_proj = None

    # Check shape type
    if isinstance(shp, ogr.Geometry):
        geom = shp

    elif isinstance(shp, ogr.Feature):
        geom = shp.geometry()

    elif isinstance(shp, BaseGeometry):
        geom = ogr.CreateGeometryFromWkb(wkb.dumps(shp))
    else:
        raise ValueError("Unable to convert to ogr.Geometry object")

    # Check projection
    if isinstance(proj, str) or isinstance(proj, unicode):
        target_proj = SpatialReference()
        target_proj.ImportFromProj4(proj)

    elif isinstance(proj, SpatialReference):
        target_proj = proj

    elif proj is None:
        target_proj = geom.GetSpatialReference()
        if target_proj is None:
            raise ValueError("shp does not have a SpatialReference")
    else:
        raise ValueError("Unable to set projction.")

    # Return shapely
    if isinstance(shp, BaseGeometry):
        geom.AssignSpatialReference(proj)
        return geom

    if copy:
        geom = geom.Clone()

    if proj is not None:
        source_proj = geom.GetSpatialReference()
        if source_proj is None:
            raise ValueError("shp does not have a SpatialReference")
        ct = CoordinateTransformation(source_proj, target_proj)
        geom.Transform(ct)
        geom.AssignSpatialReference(target_proj)

    return geom