Esempio n. 1
0
def get_utm_wkt(coordinate, from_wkt):
    '''
    Function to return CRS for UTM zone of specified coordinates.
    Used to transform coords to metres
    @param coordinate: single coordinate pair
    '''
    def utm_getZone(longitude):
        return (int(1 + (longitude + 180.0) / 6.0))

    def utm_isNorthern(latitude):
        if (latitude < 0.0):
            return 0
        else:
            return 1

    coordinate_array = np.array(coordinate).reshape((1, 2))

    latlon_coord_trans = get_coordinate_transformation(from_wkt, 'EPSG:4326')
    latlon_coord = coordinate if latlon_coord_trans is None else latlon_coord_trans.TransformPoints(
        coordinate_array)[0][0:2]

    # Set UTM coordinate reference system
    utm_spatial_ref = SpatialReference()
    utm_spatial_ref.SetWellKnownGeogCS('WGS84')
    utm_spatial_ref.SetUTM(utm_getZone(latlon_coord[0]),
                           utm_isNorthern(latlon_coord[1]))

    return utm_spatial_ref.ExportToWkt()
Esempio n. 2
0
def get_spatial_ref_from_wkt(wkt_or_crs_name):
    '''
    Function to return SpatialReference object for supplied WKT
    @param wkt: Well-known text or CRS name for SpatialReference, including "EPSG:XXXX"
    @return spatial_ref: SpatialReference from WKT
    '''
    spatial_ref = SpatialReference()

    # Try to resolve WKT
    result = spatial_ref.ImportFromWkt(wkt_or_crs_name)
    if not result:
        return spatial_ref

    # Try to resolve CRS name - either mapped or original
    result = spatial_ref.SetWellKnownGeogCS(
        CRS_NAME_MAPPING.get(wkt_or_crs_name) or wkt_or_crs_name)
    if not result:
        return spatial_ref

    # Try common formulations for UTM zones
    #TODO: Fix this so it works in the Northern hemisphere
    modified_crs_name = re.sub('\s+', '', wkt_or_crs_name.strip().upper())
    utm_match = (re.match('(\w+)/MGAZONE(\d+)', modified_crs_name)
                 or re.match('(\w+)/(\d+)S', modified_crs_name)
                 or re.match('(EPSG:283)(\d{2})', modified_crs_name))
    if utm_match:
        modified_crs_name = utm_match.group(1)
        utm_zone = int(utm_match.group(2))
        result = spatial_ref.SetWellKnownGeogCS(
            CRS_NAME_MAPPING.get(modified_crs_name) or modified_crs_name)
    if not result:
        spatial_ref.SetUTM(
            utm_zone, False
        )  # Put this here to avoid potential side effects in downstream code
        return spatial_ref

    assert not result, 'Invalid WKT or CRS name'
Esempio n. 3
0
def get_spatial_ref_from_wkt(wkt_or_crs_name):
    '''
    Function to return SpatialReference object for supplied WKT
    @param wkt: Well-known text or CRS name for SpatialReference, including "EPSG:XXXX"
    @return spatial_ref: SpatialReference from WKT
    '''
    if not wkt_or_crs_name:
        return None

    spatial_ref = SpatialReference()

    result = spatial_ref.SetFromUserInput(wkt_or_crs_name)
    if not result:
        logger.debug(
            'CRS determined using SpatialReference.SetFromUserInput({})'.
            format(wkt_or_crs_name))
        return spatial_ref

    # Try to resolve WKT
    result = spatial_ref.ImportFromWkt(wkt_or_crs_name)
    if not result:
        logger.debug(
            'CRS determined using SpatialReference.ImportFromWkt({})'.format(
                wkt_or_crs_name))
        return spatial_ref

    # Try to resolve CRS name - either mapped or original
    modified_crs_name = CRS_NAME_MAPPING.get(
        wkt_or_crs_name) or wkt_or_crs_name
    result = spatial_ref.SetWellKnownGeogCS(modified_crs_name)
    if not result:
        logger.debug(
            'CRS determined using SpatialReference.SetWellKnownGeogCS({})'.
            format(modified_crs_name))
        return spatial_ref

    match = re.match('EPSG:(\d+)$', wkt_or_crs_name, re.IGNORECASE)
    if match:
        epsg_code = int(match.group(1))
        result = spatial_ref.ImportFromEPSG(epsg_code)
        if not result:
            logger.debug(
                'CRS determined using SpatialReference.ImportFromEPSG({})'.
                format(epsg_code))
            return spatial_ref

    # Try common formulations for UTM zones
    #TODO: Fix this so it works in the Northern hemisphere
    modified_crs_name = re.sub('\s+', '', wkt_or_crs_name.strip().upper())
    utm_match = (re.match('(\w+)/MGAZONE(\d+)$', modified_crs_name)
                 or re.match('(\w+)/(\d+)S$', modified_crs_name)
                 or re.match('(EPSG:283)(\d{2})$', modified_crs_name)
                 or re.match('(MGA)(\d{2}$)', modified_crs_name))
    if utm_match:
        modified_crs_name = utm_match.group(1)
        modified_crs_name = CRS_NAME_MAPPING.get(
            modified_crs_name) or modified_crs_name
        utm_zone = int(utm_match.group(2))
        result = spatial_ref.SetWellKnownGeogCS(modified_crs_name)
    if not result:
        spatial_ref.SetUTM(
            utm_zone, False
        )  # Put this here to avoid potential side effects in downstream code
        logger.debug(
            'UTM CRS determined using SpatialReference.SetWellKnownGeogCS({}) (zone{})'
            .format(modified_crs_name, utm_zone))
        return spatial_ref

    assert not result, 'Invalid WKT or CRS name: "{}"'.format(wkt_or_crs_name)