Esempio n. 1
0
def geographic2plane(latlon, 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(geographic, plane)

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

    return xy[:2]
Esempio n. 2
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
Esempio n. 3
0
    def _inspect_data(self):
        """
        Extract coordinate reference system and min/max values from a
        GDAL supported raster data file.
        """

        if self._data is None:
            # TODO: This method is really ugly, but is nice for testing
            self._data = {}
            df = gdal.Open(self.filename)
            # TODO: df may be None?
            crs = df.GetProjection()
            if crs:
                spref = SpatialReference(wkt=crs)
                # default to epsg:4326
                auth = spref.GetAuthorityName(None) or 'epsg'
                code = spref.GetAuthorityCode(None) or '4326'
                self._data['crs'] = "%s:%s" % (auth.lower(), code)
                # LOG.info('Detected CRS: %s', self._data['crs'])
            else:
                self._data['crs'] = 'epsg:4326'  # use epsg:4326 as default
            band = df.GetRasterBand(1)
            self._data['min'], self._data['max'], _, _ = band.GetStatistics(
                True, False)
            self._data['nodata'] = band.GetNoDataValue()
            self._data['datatype'] = band.DataType
Esempio n. 4
0
def read_raster(path, band_number=1):
    """
    Create a raster dataset from a single raster file

    Parameters
    ----------
    path: string
        Path to the raster file.  Can be either local or s3/gs.

    band_number: int
        The band number to use

    Returns
    -------

    RasterDataset
    """

    path = fileutils.get_path(path)
    ds = gdal.Open(path, GA_ReadOnly)
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize
    proj = SpatialReference()
    proj.ImportFromWkt(ds.GetProjection())
    geo_transform = ds.GetGeoTransform()
    return RasterDataset(ds, xsize, ysize, geo_transform, proj)
Esempio n. 5
0
    def write_esri_grid_ascii_file(path,
                                   data,
                                   extent,
                                   cellsize,
                                   nodata_value=-9999.0):
        xllcorner, yllcorner, *_ = extent.as_list()
        nrows, ncols = data.shape

        header_dict = OrderedDict()
        header_dict['nrows'] = nrows
        header_dict['ncols'] = ncols
        header_dict['xllcorner'] = xllcorner
        header_dict['yllcorner'] = yllcorner
        header_dict['cellsize'] = cellsize
        header_dict['nodata_value'] = nodata_value
        header = '\n'.join(
            ['{} {}'.format(key, val) for key, val in header_dict.items()])

        numpy.savetxt(path, data, header=header, comments='')

        # save projection info to adjacent file
        ref = SpatialReference()
        ref.ImportFromProj4(extent.projection.srs)
        with open(path.replace('asc', 'prj'), 'w') as f:
            f.write(ref.ExportToWkt())
Esempio n. 6
0
    def envelope(self):

        dataset = self.getDataset()
        xform = dataset.GetGeoTransform()
        xScale = xform[1]
        yScale = xform[5]
        width = dataset.RasterXSize
        height = dataset.RasterYSize
        ulx = xform[0]
        uly = xform[3]
        lrx = ulx + width * xScale
        lry = uly + height * yScale

        envelope = Envelope()

        # ---
        # If this file is in 4326, we must swap the x-y to conform with GDAL
        # 3's strict conformity to the 4326 definition.
        # ---
        srs4326 = SpatialReference()
        srs4326.ImportFromEPSG(4326)

        if srs4326.IsSame(self._dataset.GetSpatialRef()):

            envelope.addPoint(uly, ulx, 0, self._dataset.GetSpatialRef())
            envelope.addPoint(lry, lrx, 0, self._dataset.GetSpatialRef())

        else:

            envelope.addPoint(ulx, uly, 0, self._dataset.GetSpatialRef())
            envelope.addPoint(lrx, lry, 0, self._dataset.GetSpatialRef())

        return envelope
Esempio n. 7
0
    def testExpandByPercentage(self):

        ulx = 374187
        uly = 4202663
        lrx = 501598
        lry = 4100640
        srs = SpatialReference()
        srs.ImportFromEPSG(32612)
        env = Envelope()
        env.addPoint(ulx, uly, 0, srs)
        env.addPoint(lrx, lry, 0, srs)

        env.expandByPercentage(0.0)
        self.assertEqual(ulx, env.ulx())
        self.assertEqual(uly, env.uly())
        self.assertEqual(lrx, env.lrx())
        self.assertEqual(lry, env.lry())

        percentage = 10
        width = 127411.0
        height = 163224.55529116935
        exWidth = width * percentage / 100 / 2.0
        exHeight = height * percentage / 100 / 2.0
        exUlx = abs(ulx - exWidth)
        exUly = abs(uly + exHeight)
        newUlx, newUly, newLrx, newLry = env.expandByPercentage(percentage)
        self.assertEqual(exUlx, newUlx)
        self.assertEqual(exUly, newUly)
Esempio n. 8
0
def _nor_roms(xp=3991,
              yp=2230,
              dx=800,
              ylon=70,
              name='NK800',
              metric_unit=False):
    if metric_unit:
        unit_str = 'UNIT["metre",1,AUTHORITY["EPSG","9001"]]'
        dx_unit = dx
    else:
        unit_str = f'UNIT["Cells",{dx}]]'
        dx_unit = 1

    wkt = f"""PROJCS["{name}",
        GEOGCS["ETRS89",
            DATUM["European_Terrestrial_Reference_System_1989",
                SPHEROID["GRS 1980",6378137,298.257222101,
                    AUTHORITY["EPSG","7019"]],
                AUTHORITY["EPSG","6258"]],
            PRIMEM["Greenwich",0,
                AUTHORITY["EPSG","8901"]],
            UNIT["degree",0.01745329251994328,
                AUTHORITY["EPSG","9122"]],
            AUTHORITY["EPSG","4258"]],
        PROJECTION["Polar_Stereographic"],
        PARAMETER["latitude_of_origin",60],
        PARAMETER["central_meridian",{ylon}],
        PARAMETER["scale_factor",1],
        PARAMETER["false_easting",{xp * dx_unit}],
        PARAMETER["false_northing",{yp * dx_unit}],
        {unit_str}"""

    sr = SpatialReference()
    sr.ImportFromWkt(wkt)
    return sr
Esempio n. 9
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. 10
0
def pyproj_crs_to_osgeo(proj_crs: Union[CRS, int]):
    """
    Convert from the pyproj CRS object to osgeo SpatialReference

    See https://pyproj4.github.io/pyproj/stable/crs_compatibility.html

    Parameters
    ----------
    proj_crs
        pyproj CRS or an integer epsg code

    Returns
    -------
    SpatialReference
        converted SpatialReference
    """

    if isinstance(proj_crs, int):
        proj_crs = CRS.from_epsg(proj_crs)
    osr_crs = SpatialReference()
    if osgeo.version_info.major < 3:
        osr_crs.ImportFromWkt(proj_crs.to_wkt(WktVersion.WKT1_GDAL))
    else:
        osr_crs.ImportFromWkt(proj_crs.to_wkt())
    return osr_crs
Esempio n. 11
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]
Esempio n. 12
0
    def load_data(self):
        filename = self.path.split(os.sep)[-1]

        # Check for VELMA filename matches
        for pattern in [self.VELMA_FILENAME_BASIC_RE, self.VELMA_FILENAME_NO_LAYER_RE,
                        self.VELMA_FILENAME_SUBDAY_RE, self.VELMA_FILENAME_SUBDAY_NO_LAYER_RE]:
            match = pattern.match(filename)
            if match:
                self._velma_pattern = pattern
                self.data_name = match.group(1)
                self._loop = match.group(2)

                self._has_layer = pattern in [self.VELMA_FILENAME_BASIC_RE, self.VELMA_FILENAME_SUBDAY_RE]
                if self._has_layer:
                    self._layer = match.group(3)
                self._is_subday = pattern in [self.VELMA_FILENAME_SUBDAY_RE, self.VELMA_FILENAME_SUBDAY_NO_LAYER_RE]
                self._is_velma = True
                break

        projection = None
        prj_file = self.path.replace('.asc', '.prj')
        if os.path.exists(prj_file):
            with open(prj_file, 'r') as f:
                ref = SpatialReference()
                ref.ImportFromWkt(f.read())
                projection = Proj(ref.ExportToProj4())

        # Capture georeference
        with rasterio.open(self.path) as src:
            self.affine = src.transform
            self.shape = src.shape
            self.resolution = src.res[0]    # Assumed to be square
            self.extent = Extent(*list(src.bounds), projection=projection)
            self._nodata_value = src.nodata

        self.time_info = TemporalInfo()
        if not self._is_velma:
            self.data_name = self.path.split(os.sep)[-1].split('.')[0]
            return

        timestamps = []
        for f in os.listdir(os.path.dirname(os.path.abspath(self.path))):
            filename = f.split(os.sep)[-1]
            match = self._velma_pattern.match(filename)

            # Skip if match is None and if match name or loop is wrong
            if match and match.group(1) == self.data_name and match.group(2) == self._loop:

                # Skip if match layer is wrong, if applicable
                if self._has_layer and match.group(3) != self._layer:
                    continue

                years = int(match.group(4 if self._has_layer else 3))
                days = int(match.group(5 if self._has_layer else 4))
                hours = int(match.group(6 if self._has_layer else 5)) if self._is_subday else 0
                minutes = int(match.group(7 if self._has_layer else 6)) if self._is_subday else 0
                insort(timestamps, datetime.datetime(years, 1, 1, hours, minutes) + datetime.timedelta(days - 1))

        self.time_info.timestamps = timestamps
Esempio n. 13
0
    def _extractVars(files, variables, envelope, outDir):

        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
        subFiles = []

        for f in files:

            geoFile = GeospatialImageFile(f, spatialReference=srs)
            subs = geoFile.getDataset().GetSubDatasets()
            foundVariables = []

            # Look for a variable name in the subdataset name.
            for sub in subs:

                datasetName = sub[0]
                var = datasetName.split(':')[2]

                if var in variables:

                    foundVariables.append(var)

                    # ---
                    # Copy the original file before operating on it, unless
                    # it already exists in the output directory.
                    # ---
                    name = os.path.basename(os.path.splitext(f)[0]) + \
                        '_' + \
                        var + \
                        '.nc'

                    workingCopy = os.path.join(outDir, name)

                    if not os.path.exists(workingCopy):

                        shutil.copyfile(f, workingCopy)

                        # Extract and clip the subdataset.
                        copyGeoFile = GeospatialImageFile(workingCopy, srs)
                        copyGeoFile.clipReproject(envelope, None, sub[0])

                    else:
                        copyGeoFile = GeospatialImageFile(workingCopy, srs)

                    subFiles.append(copyGeoFile.fileName())

                    # Stop, when all variables are found.
                    if len(foundVariables) == len(variables):
                        break

            # Are any variables missing?
            if len(foundVariables) != len(variables):

                missing = [v for v in variables if v not in foundVariables]
                msg = 'Variables not found in ' + str(f) + ': ' + str(missing)
                raise RuntimeError(msg)

        return subFiles
Esempio n. 14
0
    def gdal(self):
        if self._gdal is not None:
            pass
        elif self._proj4 is not None:
            self._gdal = SpatialReference()
            self._gdal.ImportFromProj4(self._proj4)

        return self._gdal
Esempio n. 15
0
    def __setstate__(self, state):

        srs = SpatialReference()
        srs.ImportFromProj4(state[GeospatialImageFile.SRS_KEY])

        self.__init__(state[GeospatialImageFile.FILE_KEY], srs,
                      state[GeospatialImageFile.SUBDATASET_KEY],
                      state[GeospatialImageFile.LOGGER_KEY])
Esempio n. 16
0
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
Esempio n. 17
0
    def __geographic2plane(self, 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
        eo_conv = copy(eo)
        eo_conv[0:2] = xy[0:2]
        return eo_conv
Esempio n. 18
0
def alt_downloader(tile_nr_x, tile_nr_y):

    # construct url
    url = www_folder + str(tile_nr_x) + '/' + str(tile_nr_y) + '.asc.zip'

    try:
        # create a momemory driver and dataset on it
        driver = gdal.GetDriverByName('MEM')
        ds = driver.Create('', TD['NCOLS'], TD['NROWS'], 1, gdal.GDT_Float32)

        # access the zip file
        zf = ZipFile(BytesIO(requests.get(url).content))

        # read the rasterfile in the format of an array
        lines = zf.open(zf.infolist()[0]).readlines()

        # NOTE:
        # in the following lines we use some properties that the .asc files on our server have:
        # - carriage returns are used to separate header items and rows
        # - the header is 6 lines, that is, there is a NO DATA value in line 6
        # - the data starts in line 7 (index 6)
        # these are not required by the standard, c.f.
        # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/ESRI_ASCII_raster_format/009t0000000z000000/
        # in particular, NO DATA is optional and carriage returns may be replaced by spaces

        # read the geo transform

        #as from http://geoexamples.blogspot.com/2012/01/creating-files-in-ogr-and-gdal-with.html:

        #geotransform = (left x-coordinate, x-cellsize, rotation ?,upper y-coordinate,rotation,y-cellsize)

        #Xgeo = geotransform[0] + Xpixel*geotransform[1] + Yline*geotransform[2]
        #Ygeo = geotransform[3] + Xpixel*geotransform[4] + Yline*geotransform[5]

        #for some reason, y-cellsize must be negative here

        geo_trafo = (float(lines[2].split()[1]), TD['CELLSIZE'], 0,
                     float(lines[3].split()[1]) + TD['CELLSIZE'] * TD['NROWS'],
                     0, -TD['CELLSIZE'])

        ds.SetGeoTransform(geo_trafo)

        # read and write the data to the dataset
        arr = list(map(lambda x: list(map(float, x.split())), lines[6:]))
        zf.close()
        band = ds.GetRasterBand(1)
        band.WriteArray(array(arr))

        # set the spatial reference system (probably not necessary)
        proj = SpatialReference()
        proj.SetWellKnownGeogCS("EPSG:31287")
        ds.SetProjection(proj.ExportToWkt())

        return ds

    except:
        return None
Esempio n. 19
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)
Esempio n. 20
0
    def testSRS(self):

        testSRS = SpatialReference()
        testSRS.ImportFromEPSG(32612)

        obs = ObservationFile(ObservationFileTestCase._testObsFile,
                              ObservationFileTestCase._species)

        self.assertTrue(obs.srs().IsSame(testSRS))
Esempio n. 21
0
def crs_from_gridmapping(grid_mapping):
    """Create projection from grid_mapping variable"""
    if 'crs_wkt' not in grid_mapping.attrs:
        raise NotImplementedError('At present, a "crs_wkt" attr is required')

    wkt = grid_mapping.attrs['crs_wkt']
    sr = SpatialReference()
    sr.ImportFromWkt(wkt)
    return sr
Esempio n. 22
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]))
    converted_eo = copy(eo)
    converted_eo[0:2] = xy[0:2]

    return converted_eo
Esempio n. 23
0
 def __init__(self,*args,**kwds):
     self.ocgis = ocgis(self)
     sr = kwds.pop('sr',None)
     if sr is None:
         sr = SpatialReference()
         sr.ImportFromEPSG(4326)
     self.ocgis._proj4_str = sr.ExportToProj4()
     
     super(SelectionGeometry,self).__init__(*args,**kwds)
Esempio n. 24
0
    def __new__(cls, ds, band_number=1):
        """
        Create an in-memory representation for a single band in
        a raster. (0,0) in pixel coordinates represents the
        upper left corner of the raster which corresponds to
        (min_lon, max_lat).  Inherits from ndarray, so you can
        use it like a numpy array.

        Parameters
        ----------
        ds: gdal.Dataset

        band_number: int
            The band number to use

        Attributes
        ----------
        data: np.ndarray[xsize, ysize]
             The raster data
        """

        if not isinstance(ds, gdal.Dataset):
            path = fileutils.get_path(ds)
            ds = gdal.Open(path, GA_ReadOnly)

        band = ds.GetRasterBand(band_number)
        if band is None:
            msg = "Unable to load band %d " % band_number
            msg += "in raster %s" % ds.GetDescription()
            raise ValueError(msg)

        gdal_type = band.DataType
        dtype = np.dtype(GDAL2NP_CONVERSION[gdal_type])
        self = np.asarray(band.ReadAsArray().astype(dtype)).view(cls)

        self.gdal_type = gdal_type
        proj = SpatialReference()
        proj.ImportFromWkt(ds.GetProjection())
        geo_transform = ds.GetGeoTransform()

        # Initialize the base class with coordinate information.
        RasterBase.__init__(self, ds.RasterXSize, ds.RasterYSize,
                            geo_transform, proj)

        self.nan = band.GetNoDataValue()

        #self = np.ma.masked_equal(self, band.GetNoDataValue(), copy=False)
        ctable = band.GetColorTable()
        if ctable is not None:
            self.colors = np.array(
                [ctable.GetColorEntry(i) for i in range(256)], dtype=np.uint8)
        else:
            self.colors = None

        ds = None
        return self
Esempio n. 25
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]])
Esempio n. 26
0
    def srs(self):

        srs = self._dataset.GetSpatialRef()

        if not srs:
            srs = SpatialReference()
            srs.ImportFromEPSG(4326)
            srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        return srs
Esempio n. 27
0
    def __setstate__(self, state):

        multipointWkt = state[Envelope.MULTIPOINT_KEY]
        copy = ogr.CreateGeometryFromWkt(multipointWkt)
        self.__dict__.update(copy.__dict__)

        srsProj4 = state[Envelope.SRS_KEY]
        srs = SpatialReference()
        srs.ImportFromProj4(srsProj4)
        self.AssignSpatialReference(srs)
Esempio n. 28
0
    def _createTestFile(self):

        testFile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                'TSURF.nc')

        workingCopy = tempfile.mkstemp(suffix='.nc')[1]
        shutil.copyfile(testFile, workingCopy)
        srs = SpatialReference()
        srs.ImportFromEPSG(4326)
        return GeospatialImageFile(workingCopy, srs)
Esempio n. 29
0
    def _createTestFile(self, createUTM=False):

        # ---
        # Set up a logger because serialization was causing loggers to point
        # to the SpatialReferences.
        # ---
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        logger.addHandler(ch)

        testFile = None

        if createUTM:

            testFile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    'gsenm_250m_eucl_dist_streams.tif')

            workingCopy = tempfile.mkstemp(suffix='.tif')[1]
            shutil.copyfile(testFile, workingCopy)

            srs = SpatialReference()
            srs.ImportFromEPSG(32612)

        else:
            testFile = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    'TSURF.nc')

            workingCopy = tempfile.mkstemp(suffix='.nc')[1]
            shutil.copyfile(testFile, workingCopy)

            # ---
            # https://github.com/OSGeo/gdal/blob/release/3.0/gdal/
            # MIGRATION_GUIDE.TXT
            # ---
            srs = SpatialReference()
            srs.ImportFromEPSG(4326)
            srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

        return GeospatialImageFile(workingCopy,
                                   spatialReference=srs,
                                   logger=logger)
Esempio n. 30
0
def get_identifier(crs):
    """
    Given a CRS, generate a unique idenfier for it. Eg: "EPSG:2193"
    """
    if isinstance(crs, str):
        crs = SpatialReference(crs)
    if isinstance(crs, SpatialReference):
        auth_name = crs.GetAuthorityName(None)
        auth_code = crs.GetAuthorityCode(None)
        return f"{auth_name}:{auth_code}"
    raise RuntimeError(f"Unrecognised CRS: {crs}")