Esempio n. 1
0
    def test_not_equal(self):
        """ Tests if two `SpatialRef` instances are not equal. """

        sref_a = SpatialRef(self.proj4, sref_type='proj4')
        sref_b = SpatialRef(4326, sref_type='epsg')

        assert sref_a != sref_b
Esempio n. 2
0
    def test_equal(self):
        """ Tests if two `SpatialRef` instances are equal. """

        sref_a = SpatialRef(self.proj4, sref_type='proj4')
        sref_b = SpatialRef(self.proj4_dict, sref_type='proj4')

        assert sref_a == sref_b
Esempio n. 3
0
    def test_create_from_proj4(self):
        """ Creates a `SpatialRef` object from a PROJ4 string and checks all conversions."""

        # special wkt string has to be defined because PROJ4 -> WKT does lead to an unnamed projection
        if not GDAL_3_ENABLED:
            wkt = 'PROJCS["unnamed",GEOGCS["Bessel 1841",DATUM["unknown",SPHEROID["bessel",6377397.155,299.1528128],' \
                  'TOWGS84[577.326,90.129,463.919,5.137,1.474,5.297,2.4232]],PRIMEM["Greenwich",0],' \
                  'UNIT["degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],' \
                  'PARAMETER["central_meridian",16.33333333333333],PARAMETER["scale_factor",1],' \
                  'PARAMETER["false_easting",750000],PARAMETER["false_northing",-5000000],UNIT["Meter",1]]'
        else:
            wkt = 'PROJCS["unknown",GEOGCS["unknown",DATUM["Unknown_based_on_Bessel_1841_ellipsoid",' \
                  'SPHEROID["Bessel 1841",6377397.155,299.1528128],' \
                  'TOWGS84[577.326,90.129,463.919,5.137,1.474,5.297,2.4232]],PRIMEM["Greenwich",0,' \
                  'AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],' \
                  'PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],' \
                  'PARAMETER["central_meridian",16.3333333333333],PARAMETER["scale_factor",1],' \
                  'PARAMETER["false_easting",750000],PARAMETER["false_northing",-5000000],UNIT["metre",1,' \
                  'AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'
        epsg = None  # None because EPSG <-> PROJ4 transformation is not possible/is ambiguous

        sref = SpatialRef(self.proj4)
        self.assertListEqual([epsg, wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])

        sref = SpatialRef(self.proj4, sref_type="proj4")
        self.assertListEqual([epsg, wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])
Esempio n. 4
0
    def test_create_from_epsg(self):
        """ Creates a `SpatialRef` object from an EPSG code and checks all conversions. """

        sref = SpatialRef(self.epsg)
        self.assertListEqual([self.epsg, self.wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])

        sref = SpatialRef(self.epsg, sref_type="epsg")
        self.assertListEqual([self.epsg, self.wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])
Esempio n. 5
0
    def test_create_from_wkt(self):
        """ Creates a `SpatialRef` object from a WKT string and checks all conversions."""

        sref = SpatialRef(self.wkt)
        self.assertListEqual([self.epsg, self.wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])

        sref = SpatialRef(self.wkt, sref_type="wkt")
        self.assertListEqual([self.epsg, self.wkt, self.proj4],
                             [sref.epsg, sref.wkt, sref.proj4])
Esempio n. 6
0
    def setUp(self):
        """ Creates a normal and a rotated raster geometry with a random coordinate extent. """

        # set up spatial reference system
        self.sref = SpatialRef(4326)

        # define region of interest/extent
        ll_x = random.randrange(-50., 50., 10.)
        ll_y = random.randrange(-50., 50., 10.)
        ur_x = ll_x + random.randrange(10., 50., 10.)
        ur_y = ll_y + random.randrange(10., 50., 10.)
        self.sh_geom = Polygon(((ll_x, ll_y), (ll_x, ur_y), (ur_x, ur_y),
                                (ur_x, ll_y)))  # Polygon in clock-wise order
        self.ogr_geom = ogr.CreateGeometryFromWkt(self.sh_geom.wkt)
        self.ogr_geom.AssignSpatialReference(self.sref.osr_sref)

        self.extent = tuple(map(float, (ll_x, ll_y, ur_x, ur_y)))
        self.x_pixel_size = 0.5
        self.y_pixel_size = 0.5
        self.raster_geom = RasterGeometry.from_extent(self.extent, self.sref,
                                                      self.x_pixel_size,
                                                      self.y_pixel_size)

        # create rotated raster geometry
        geom_nap = affinity.rotate(
            shapely.wkt.loads(self.ogr_geom.ExportToWkt()), 45, 'center')
        geom_nap = ogr.CreateGeometryFromWkt(geom_nap.wkt)
        geom_nap.AssignSpatialReference(self.sref.osr_sref)
        self.raster_geom_rot = RasterGeometry.from_geometry(
            geom_nap, self.x_pixel_size, self.y_pixel_size)
Esempio n. 7
0
    def setUp(self):
        """ Sets up a `RegularMosaicGeometry` object with a random, upper-left origin. """

        # define spatial reference
        sref = SpatialRef(4326)
        # define pixel spacing
        x_pixel_size = 0.01
        y_pixel_size = 0.01
        # define origin and number of tiles (randomly)
        mosaic_ul_x = random.randrange(-50., 50., 10.)
        mosaic_ul_y = random.randrange(-50., 50., 10.)
        mosaic_rows = 3
        mosaic_cols = 3
        x_tile_size = 1.
        y_tile_size = 1.
        # define geotrans
        geotrans = (mosaic_ul_x, x_pixel_size, 0., mosaic_ul_y, 0.,
                    -y_pixel_size)

        self.mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(
            mosaic_rows,
            mosaic_cols,
            x_tile_size,
            y_tile_size,
            sref,
            geotrans=geotrans)
Esempio n. 8
0
    def test_to_pretty_wkt(self):
        """ Tests pretty Well Known Text (WKT) export. """

        pretty_wkt = 'PROJCS["MGI / Austria GK M34",\n    GEOGCS["MGI",\n        ' \
                     'DATUM["Militar_Geographische_Institute",\n            ' \
                     'SPHEROID["Bessel 1841",6377397.155,299.1528128,\n                ' \
                     'AUTHORITY["EPSG","7004"]],\n            ' \
                     'TOWGS84[577.326,90.129,463.919,5.137,1.474,5.297,2.4232],\n            ' \
                     'AUTHORITY["EPSG","6312"]],\n        PRIMEM["Greenwich",0,\n            ' \
                     'AUTHORITY["EPSG","8901"]],\n        UNIT["degree",0.0174532925199433,\n            ' \
                     'AUTHORITY["EPSG","9122"]],\n        AUTHORITY["EPSG","4312"]],\n    ' \
                     'PROJECTION["Transverse_Mercator"],\n    PARAMETER["latitude_of_origin",0],\n    ' \
                     'PARAMETER["central_meridian",16.33333333333333],\n    PARAMETER["scale_factor",1],\n    ' \
                     'PARAMETER["false_easting",750000],\n    PARAMETER["false_northing",-5000000],\n    ' \
                     'UNIT["metre",1,\n        AUTHORITY["EPSG","9001"]],\n    AUTHORITY["EPSG","31259"]]'

        sref = SpatialRef(self.epsg)
        assert sref.to_pretty_wkt() == pretty_wkt
Esempio n. 9
0
    def test_to_cartopy_proj(self):
        """  Tests Cartopy projection creation from a `SpatialRef` instance. """
        sref = SpatialRef(4326)
        capy_proj = sref.to_cartopy_proj()
        assert isinstance(capy_proj, ccrs.PlateCarree)

        sref = SpatialRef(self.epsg)
        capy_proj = sref.to_cartopy_proj()
        assert isinstance(capy_proj, ccrs.TransverseMercator)

        sref = SpatialRef(102018)
        if not GDAL_3_ENABLED:
            capy_proj = sref.to_cartopy_proj()
            assert isinstance(capy_proj, ccrs.Stereographic)
        else:
            try:
                _ = sref.to_cartopy_proj()
                assert False
            except ValueError:
                assert True
Esempio n. 10
0
    def test_from_osr(self):
        """ Tests `SpatialRef` class creation from an OSR spatial reference object. """

        # create OSR spatial reference from PROJ4 string
        osr_sref = osr.SpatialReference()
        osr_sref.ImportFromProj4(self.proj4)

        # create SpatialRef object from OSR spatial reference
        sref = SpatialRef.from_osr(osr_sref)

        assert sref.proj4 == self.proj4
Esempio n. 11
0
 def test_different_sref(self):
     """ Test topological operation if the spatial reference systems are different. """
     # create raster geometry which touches the previous one
     extent_tchs = (self.extent[2], self.extent[3],
                    self.extent[2] + 5., self.extent[3] + 5.)
     raster_geom_tchs = RasterGeometry.from_extent(extent_tchs, self.sref,
                                                   self.x_pixel_size, self.y_pixel_size)
     # reproject to different system
     sref_other = SpatialRef(3857)
     geom = raster_geom_tchs.boundary_ogr
     geom.TransformTo(sref_other.osr_sref)
     assert self.raster_geom.touches(geom)
Esempio n. 12
0
    def test_plot(self):
        """ Tests plotting function of a raster geometry. """

        self.raster_geom.plot(add_country_borders=True)

        # test plotting with labelling and different output projection
        self.raster_geom.id = "E048N018T1"
        self.raster_geom.plot(proj=ccrs.EckertI(), label_geom=True, add_country_borders=True)

        # test plotting with different input projection
        sref = SpatialRef(31259)
        extent = [527798, 94878, 956835, 535687]
        raster_geom = RasterGeometry.from_extent(extent, sref, x_pixel_size=500, y_pixel_size=500)
        raster_geom.plot(add_country_borders=True)
Esempio n. 13
0
    def test_to_cartopy_proj(self):
        """  Tests Cartopy projection creation from a `SpatialRef` instance. """
        sref = SpatialRef(4326)
        capy_proj = sref.to_cartopy_proj()
        assert isinstance(capy_proj, ccrs.PlateCarree)

        sref = SpatialRef(self.epsg)
        capy_proj = sref.to_cartopy_proj()
        assert isinstance(capy_proj, ccrs.Projection)
Esempio n. 14
0
    def setUp(self):
        """
        Sets up a `MosaicGeometry` object.

        """
        # define spatial reference
        sref = SpatialRef(4326)
        # define pixel spacing
        x_pixel_size = 0.01
        y_pixel_size = 0.01
        # define origin and number of tiles (randomly)
        mosaic_ul_x = random.randrange(-50., 50., 10.)
        mosaic_ul_y = random.randrange(-50., 50., 10.)
        mosaic_rows = 1
        x_tile_size = 1.
        y_tile_size = 1.
        # define cols
        upper_mosaic_cols = 5
        middle_mosaic_cols = 3
        lower_mosaic_cols = 4
        # define tile size
        upper_x_tile_size = x_tile_size
        middle_x_tile_size = x_tile_size * (upper_mosaic_cols/middle_mosaic_cols)
        lower_x_tile_size = x_tile_size * (upper_mosaic_cols/lower_mosaic_cols)
        # define geotrans
        upper_geotrans = (mosaic_ul_x, x_pixel_size, 0., mosaic_ul_y, 0., -y_pixel_size)
        middle_geotrans = (mosaic_ul_x, x_pixel_size, 0., mosaic_ul_y - y_tile_size, 0., -y_pixel_size)
        lower_geotrans = (mosaic_ul_x, x_pixel_size, 0., mosaic_ul_y - 2*y_tile_size, 0., -y_pixel_size)

        upper_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(mosaic_rows, upper_mosaic_cols,
                                                                              upper_x_tile_size, y_tile_size,
                                                                              sref, geotrans=upper_geotrans)
        middle_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(mosaic_rows, middle_mosaic_cols,
                                                                               middle_x_tile_size, y_tile_size,
                                                                               sref, geotrans=middle_geotrans)
        lower_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(mosaic_rows, lower_mosaic_cols,
                                                                              lower_x_tile_size, y_tile_size,
                                                                              sref, geotrans=lower_geotrans)

        # merge tiles
        tiles = upper_mosaic_geom.tiles + middle_mosaic_geom.tiles + lower_mosaic_geom.tiles

        # relabel tile IDs
        for i, tile in enumerate(tiles):
            tile.id = i + 1

        self.mosaic_geom = MosaicGeometry(tiles)
Esempio n. 15
0
    def test_plot(self):
        """
        Tests the `plot` function of a raster geometry. This test is only performed,
        if matplotlib is installed.

        """

        if 'matplotlib' in sys.modules:
            self.raster_geom.plot(add_country_borders=True)

            # test plotting with labelling and different output projection
            self.raster_geom.name = "E048N018T1"
            self.raster_geom.plot(proj=ccrs.EckertI(),
                                  label_geom=True,
                                  add_country_borders=True)

            # test plotting with different input projection
            sref = SpatialRef(31259)
            extent = [527798, 94878, 956835, 535687]
            raster_geom = RasterGeometry.from_extent(extent,
                                                     sref,
                                                     x_pixel_size=500,
                                                     y_pixel_size=500)
            raster_geom.plot(add_country_borders=True)
Esempio n. 16
0
    def test_proj4_dict(self):
        """ Tests the export of a PROJ4 string to a dictionary representation. """

        sref = SpatialRef(self.proj4, sref_type="proj4")
        assert sref.to_proj4_dict() == self.proj4_dict
Esempio n. 17
0
    def setUp(self):
        """ Sets up a `MosaicGeometry` object with a random, upper-left origin. """

        # define spatial reference
        sref = SpatialRef(4326)
        # define pixel spacing
        x_pixel_size = 0.01
        y_pixel_size = 0.01
        # define origin and number of tiles (randomly)
        mosaic_ul_x = random.randrange(-50., 50., 10.)
        mosaic_ul_y = random.randrange(-50., 50., 10.)
        mosaic_rows = 1
        y_tile_size = 1.
        # define cols
        upper_mosaic_cols = 5
        middle_mosaic_cols = 3
        lower_mosaic_cols = 4
        # define tile size
        upper_x_tile_size = 1.
        middle_x_tile_size = 1.8
        lower_x_tile_size = 1.2
        # define geotrans
        upper_geotrans = (mosaic_ul_x, x_pixel_size, 0., mosaic_ul_y, 0.,
                          -y_pixel_size)
        middle_geotrans = (mosaic_ul_x, x_pixel_size, 0.,
                           mosaic_ul_y - y_tile_size, 0., -y_pixel_size)
        lower_geotrans = (mosaic_ul_x, x_pixel_size, 0.,
                          mosaic_ul_y - 2 * y_tile_size, 0., -y_pixel_size)

        metadata_1 = {'test': 1}
        upper_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(
            mosaic_rows,
            upper_mosaic_cols,
            upper_x_tile_size,
            y_tile_size,
            sref,
            geotrans=upper_geotrans,
            name_frmt="R1S{:03d}W{:03d}",
            tile_kwargs={'metadata': metadata_1})
        metadata_2 = {'test': 2}
        middle_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(
            mosaic_rows,
            middle_mosaic_cols,
            middle_x_tile_size,
            y_tile_size,
            sref,
            geotrans=middle_geotrans,
            name_frmt="R2S{:03d}W{:03d}",
            tile_kwargs={'metadata': metadata_2})
        metadata_3 = {'test': 3}
        lower_mosaic_geom = RegularMosaicGeometry.from_rectangular_definition(
            mosaic_rows,
            lower_mosaic_cols,
            lower_x_tile_size,
            y_tile_size,
            sref,
            geotrans=lower_geotrans,
            name_frmt="R3S{:03d}W{:03d}",
            tile_kwargs={'metadata': metadata_3})

        # merge tiles
        tiles = upper_mosaic_geom.tiles + middle_mosaic_geom.tiles + lower_mosaic_geom.tiles
        self.mosaic_geom = MosaicGeometry(tiles)