Exemplo n.º 1
0
 def srs(self):
     "Return the Spatial Reference used in this Layer."
     try:
         ptr = capi.get_layer_srs(self.ptr)
         return SpatialReference(srs_api.clone_srs(ptr))
     except SRSException:
         return None
Exemplo n.º 2
0
    def transform(self, coord_trans, clone=False):
        """
        Transform this geometry to a different spatial reference system.
        May take a CoordTransform object, a SpatialReference object, string
        WKT or PROJ.4, and/or an integer SRID.  By default, return nothing
        and transform the geometry in-place. However, if the `clone` keyword is
        set, return a transformed clone of this geometry.
        """
        if clone:
            klone = self.clone()
            klone.transform(coord_trans)
            return klone

        # Depending on the input type, use the appropriate OGR routine
        # to perform the transformation.
        if isinstance(coord_trans, CoordTransform):
            capi.geom_transform(self.ptr, coord_trans.ptr)
        elif isinstance(coord_trans, SpatialReference):
            capi.geom_transform_to(self.ptr, coord_trans.ptr)
        elif isinstance(coord_trans, (int, str)):
            sr = SpatialReference(coord_trans)
            capi.geom_transform_to(self.ptr, sr.ptr)
        else:
            raise TypeError('Transform only accepts CoordTransform, '
                            'SpatialReference, string, and integer objects.')
Exemplo n.º 3
0
 def _get_srs(self):
     "Return the Spatial Reference for this Geometry."
     try:
         srs_ptr = capi.get_geom_srs(self.ptr)
         return SpatialReference(srs_api.clone_srs(srs_ptr))
     except SRSException:
         return None
Exemplo n.º 4
0
 def _from_json(geom_input):
     ptr = capi.from_json(geom_input)
     if GDAL_VERSION < (2, 0):
         try:
             capi.get_geom_srs(ptr)
         except SRSException:
             srs = SpatialReference(4326)
             capi.assign_srs(ptr, srs.ptr)
     return ptr
Exemplo n.º 5
0
 def srs(self):
     """
     Return the SpatialReference used in this GDALRaster.
     """
     try:
         wkt = capi.get_ds_projection_ref(self._ptr)
         if not wkt:
             return None
         return SpatialReference(wkt, srs_type='wkt')
     except SRSException:
         return None
Exemplo n.º 6
0
 def srs(self, value):
     """
     Set the spatial reference used in this GDALRaster. The input can be
     a SpatialReference or any parameter accepted by the SpatialReference
     constructor.
     """
     if isinstance(value, SpatialReference):
         srs = value
     elif isinstance(value, (int, str)):
         srs = SpatialReference(value)
     else:
         raise ValueError('Could not create a SpatialReference from input.')
     capi.set_ds_projection_ref(self._ptr, srs.wkt.encode())
     self._flush()
Exemplo n.º 7
0
 def _set_srs(self, srs):
     "Set the SpatialReference for this geometry."
     # Do not have to clone the `SpatialReference` object pointer because
     # when it is assigned to this `OGRGeometry` it's internal OGR
     # reference count is incremented, and will likewise be released
     # (decremented) when this geometry's destructor is called.
     if isinstance(srs, SpatialReference):
         srs_ptr = srs.ptr
     elif isinstance(srs, (int, str)):
         sr = SpatialReference(srs)
         srs_ptr = sr.ptr
     elif srs is None:
         srs_ptr = None
     else:
         raise TypeError(
             'Cannot assign spatial reference with object of type: %s' %
             type(srs))
     capi.assign_srs(self.ptr, srs_ptr)
Exemplo n.º 8
0
    def transform(self, srid, driver=None, name=None, resampling='NearestNeighbour',
                  max_error=0.0):
        """
        Return a copy of this raster reprojected into the given SRID.
        """
        # Convert the resampling algorithm name into an algorithm id
        algorithm = GDAL_RESAMPLE_ALGORITHMS[resampling]

        # Instantiate target spatial reference system
        target_srs = SpatialReference(srid)

        # Create warped virtual dataset in the target reference system
        target = capi.auto_create_warped_vrt(
            self._ptr, self.srs.wkt.encode(), target_srs.wkt.encode(),
            algorithm, max_error, c_void_p()
        )
        target = GDALRaster(target)

        # Construct the target warp dictionary from the virtual raster
        data = {
            'srid': srid,
            'width': target.width,
            'height': target.height,
            'origin': [target.origin.x, target.origin.y],
            'scale': [target.scale.x, target.scale.y],
            'skew': [target.skew.x, target.skew.y],
        }

        # Set the driver and filepath if provided
        if driver:
            data['driver'] = driver

        if name:
            data['name'] = name

        # Warp the raster into new srid
        return self.warp(data, resampling=resampling, max_error=max_error)