コード例 #1
0
    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
ファイル: ATL08_h5ToShp.py プロジェクト: pahbs/HRSI
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
コード例 #4
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('.')])
コード例 #5
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]
コード例 #6
0
class Geofile(object):

    FILENAME = '/tmp/00/mosaic-500m.TIF'

    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('.')])

    def getValue(self, lat, lon):
        if self.gdalMajorVer < 3:
            xy = self.ct.TransformPoint(lon, lat)
        else:
            xy = self.ct.TransformPoint(
                lat, lon
            )  # since gdal ver.3 they flipped order of the arguments (!)

        x = (xy[0] - self.geotransform[0]) / self.geotransform[
            1]  # geotransform : (ulx, xres, xskew, uly, yskew, yres)
        y = (xy[1] - self.geotransform[3]) / self.geotransform[5]

        if 0 <= x < self.dataset.RasterXSize and 0 <= y < self.dataset.RasterYSize:
            try:  # in case raster isn't full extent
                structval = self.band.ReadRaster(xoff=int(x),
                                                 yoff=int(y),
                                                 xsize=1,
                                                 ysize=1,
                                                 buf_type=self.band.DataType)
                intval = struct.unpack('f', structval)  # assume float
                val = intval[0]

            except:
                val = None

        else:
            val = None

        return val
コード例 #7
0
ファイル: EoData.py プロジェクト: xiao131/Digital_Orthophoto
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
コード例 #8
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)
コード例 #9
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]
コード例 #10
0
def transform_extent(extent: GeoRectangle,
                     transform: osr.CoordinateTransformation,
                     sample_count: int = 1000) -> GeoRectangle:
    """ returns a transformed extent by transforming sample_count points along a given extent """
    if transform is None:
        return extent
    maxf = float("inf")
    (out_min_x, out_max_x, out_min_y, out_max_y) = (maxf, -maxf, maxf, -maxf)

    dx, dy = calc_dx_dy_from_extent_and_count(extent, sample_count)
    if dx == 0:
        return GeoRectangle.empty()

    y = float(extent.min_y)
    while y <= extent.max_y + dy:
        x = float(extent.min_x)
        while x <= extent.max_x + dx:
            tx, ty, tz = transform.TransformPoint(x, y)
            x += dx
            if not math.isfinite(tz):
                continue
            out_min_x = min(out_min_x, tx)
            out_max_x = max(out_max_x, tx)
            out_min_y = min(out_min_y, ty)
            out_max_y = max(out_max_y, ty)
        y += dy

    return GeoRectangle.from_min_max(out_min_x, out_max_x, out_min_y,
                                     out_max_y)
コード例 #11
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
コード例 #12
0
ファイル: EoData.py プロジェクト: ttoming/Orthophoto_Maps
def latlon2tmcentral(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)

    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: Lon, Lat
    eo[0:2] = xy[0:2]

    return eo
コード例 #13
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)
コード例 #14
0
ファイル: EoData.py プロジェクト: yuriloves/livedronemap
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]))
    converted_eo = copy(eo)
    converted_eo[0:2] = xy[0:2]

    return converted_eo
コード例 #15
0
ファイル: lsa_5days.py プロジェクト: yancyee/TUDMaster_code
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]])
コード例 #16
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()
コード例 #17
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)
コード例 #18
0
ファイル: crs.py プロジェクト: pnsaevik/imr_maps
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
コード例 #19
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
コード例 #20
0
def convertCoordinateSystem(eo):
    # Define the TM central coordinate system (EPSG 5186)
    epsg5186 = SpatialReference()
    epsg5186.ImportFromEPSG(5186)

    # Define the world mercator coordinate system (EPSG 3857)
    epsg3857 = SpatialReference()
    epsg3857.ImportFromEPSG(3857)

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

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

    # 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: Lon, Lat
    # xy = latlon2world.TransformPoint(float(eo[0]), float(eo[1]))  # The order: Lon, Lat
    eo[0:2] = xy[0:2]

    return eo
コード例 #21
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)
コード例 #22
0
ファイル: bap_lib.py プロジェクト: ebyhu0/test_wellsaccess
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)
コード例 #23
0
class BoundaryLookup(object):
    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]

    def check_point(self, x, y):
        return (self.low_x < x < self.high_x) and (self.low_y < y <
                                                   self.high_y)

    def which_area(self, x, y):
        idx = 0
        p = Point(float(x), float(y))
        for s in self.shapely_shapes:
            if s.contains(p):
                return idx
            idx += 1
        return None

    def which_area_ordered(self, x, y):
        p = Point(float(x), float(y))
        order = self.order_search((p.x, p.y))
        for i in order:
            if self.shapely_shapes[i].contains(p):
                return i
        return None

    def order_search(self, point):
        return np.argsort([
            dist for sublist in cdist(self.rep_coords, [point])
            for dist in sublist
        ])

    def lat_lon_to_bng(self, lat, lon):
        ''' transform a latitude-longitude coordinate to BNG format '''
        try:
            res = self.latlon2bng.TransformPoint(lon, lat)
        except:
            print "Error in lat_lon_to_bng."
            return None
        return (res[0], res[1])

    def lookup_boundary(self, lat, lon):
        bng = self.lat_lon_to_bng(float(lat), float(lon))
        if bng:
            if self.check_point(*bng):
                result_idx = self.which_area(bng[0], bng[1])
                if result_idx:
                    record = self.records[result_idx]
                    return record
        return None

    def lookup_boundary_ordered(self, lat, lon):
        bng = self.lat_lon_to_bng(float(lat), float(lon))
        if bng:
            if self.check_point(*bng):
                result_idx = self.which_area_ordered(bng[0], bng[1])
                if result_idx:
                    record = self.records[result_idx]
                    return record
        return None
コード例 #24
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)
コード例 #25
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
コード例 #26
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()
コード例 #27
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()
コード例 #28
0
ファイル: geo.py プロジェクト: vinber/magrit
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
ファイル: geo.py プロジェクト: vinber/magrit
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
コード例 #30
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)