Beispiel #1
0
    def extent(self, srid=WEB_MERCATOR_SRID):
        """
        Returns bbox for layer.
        """
        if not self._bbox or self._bbox_srid != srid:
            # Get bbox for raster in original coordinates
            meta = self.metadata
            xmin = meta.uperleftx
            ymax = meta.uperlefty
            xmax = xmin + meta.width * meta.scalex
            ymin = ymax + meta.height * meta.scaley

            # Create Polygon box
            geom = OGRGeometry(Envelope((xmin, ymin, xmax, ymax)).wkt)

            # Set original srs
            if meta.srs_wkt:
                geom.srs = SpatialReference(meta.srs_wkt)
            else:
                geom.srid = meta.srid

            # Transform to requested srid
            geom.transform(srid)

            # Calculate value range for bbox
            coords = geom.coords[0]
            xvals = [x[0] for x in coords]
            yvals = [x[1] for x in coords]

            # Set bbox
            self._bbox = (min(xvals), min(yvals), max(xvals), max(yvals))
            self._bbox_srid = srid
        return self._bbox
Beispiel #2
0
    def extent(self, srid=WEB_MERCATOR_SRID):
        """
        Returns bbox for layer.
        """
        if not self._bbox or self._bbox_srid != srid:
            # Get bbox for raster in original coordinates
            meta = self.metadata
            xmin = meta.uperleftx
            ymax = meta.uperlefty
            xmax = xmin + meta.width * meta.scalex
            ymin = ymax + meta.height * meta.scaley

            # Create Polygon box
            geom = OGRGeometry(Envelope((xmin, ymin, xmax, ymax)).wkt)

            # Set original srs
            if meta.srs_wkt:
                geom.srs = SpatialReference(meta.srs_wkt)
            else:
                geom.srid = meta.srid

            # Transform to requested srid
            geom.transform(srid)

            # Calculate value range for bbox
            coords = geom.coords[0]
            xvals = [x[0] for x in coords]
            yvals = [x[1] for x in coords]

            # Set bbox
            self._bbox = (min(xvals), min(yvals), max(xvals), max(yvals))
            self._bbox_srid = srid
        return self._bbox
Beispiel #3
0
    def test_srs(self):
        "Testing OGR Geometries with Spatial Reference objects."
        for mp in self.geometries.multipolygons:
            # Creating a geometry w/spatial reference
            sr = SpatialReference('WGS84')
            mpoly = OGRGeometry(mp.wkt, sr)
            self.assertEqual(sr.wkt, mpoly.srs.wkt)

            # Ensuring that SRS is propagated to clones.
            klone = mpoly.clone()
            self.assertEqual(sr.wkt, klone.srs.wkt)

            # Ensuring all children geometries (polygons and their rings) all
            # return the assigned spatial reference as well.
            for poly in mpoly:
                self.assertEqual(sr.wkt, poly.srs.wkt)
                for ring in poly:
                    self.assertEqual(sr.wkt, ring.srs.wkt)

            # Ensuring SRS propagate in topological ops.
            a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
            b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
            diff = a.difference(b)
            union = a.union(b)
            self.assertEqual(sr.wkt, diff.srs.wkt)
            self.assertEqual(sr.srid, union.srs.srid)

            # Instantiating w/an integer SRID
            mpoly = OGRGeometry(mp.wkt, 4326)
            self.assertEqual(4326, mpoly.srid)
            mpoly.srs = SpatialReference(4269)
            self.assertEqual(4269, mpoly.srid)
            self.assertEqual('NAD83', mpoly.srs.name)

            # Incrementing through the multipolygon after the spatial reference
            # has been re-assigned.
            for poly in mpoly:
                self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
                poly.srs = 32140
                for ring in poly:
                    # Changing each ring in the polygon
                    self.assertEqual(32140, ring.srs.srid)
                    self.assertEqual('NAD83 / Texas South Central',
                                     ring.srs.name)
                    ring.srs = str(SpatialReference(4326))  # back to WGS84
                    self.assertEqual(4326, ring.srs.srid)

                    # Using the `srid` property.
                    ring.srid = 4322
                    self.assertEqual('WGS 72', ring.srs.name)
                    self.assertEqual(4322, ring.srid)

            # srs/srid may be assigned their own values, even when srs is None.
            mpoly = OGRGeometry(mp.wkt, srs=None)
            mpoly.srs = mpoly.srs
            mpoly.srid = mpoly.srid
Beispiel #4
0
    def test_srs(self):
        "Testing OGR Geometries with Spatial Reference objects."
        for mp in self.geometries.multipolygons:
            # Creating a geometry w/spatial reference
            sr = SpatialReference("WGS84")
            mpoly = OGRGeometry(mp.wkt, sr)
            self.assertEqual(sr.wkt, mpoly.srs.wkt)

            # Ensuring that SRS is propagated to clones.
            klone = mpoly.clone()
            self.assertEqual(sr.wkt, klone.srs.wkt)

            # Ensuring all children geometries (polygons and their rings) all
            # return the assigned spatial reference as well.
            for poly in mpoly:
                self.assertEqual(sr.wkt, poly.srs.wkt)
                for ring in poly:
                    self.assertEqual(sr.wkt, ring.srs.wkt)

            # Ensuring SRS propagate in topological ops.
            a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
            b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
            diff = a.difference(b)
            union = a.union(b)
            self.assertEqual(sr.wkt, diff.srs.wkt)
            self.assertEqual(sr.srid, union.srs.srid)

            # Instantiating w/an integer SRID
            mpoly = OGRGeometry(mp.wkt, 4326)
            self.assertEqual(4326, mpoly.srid)
            mpoly.srs = SpatialReference(4269)
            self.assertEqual(4269, mpoly.srid)
            self.assertEqual("NAD83", mpoly.srs.name)

            # Incrementing through the multipolygon after the spatial reference
            # has been re-assigned.
            for poly in mpoly:
                self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
                poly.srs = 32140
                for ring in poly:
                    # Changing each ring in the polygon
                    self.assertEqual(32140, ring.srs.srid)
                    self.assertEqual("NAD83 / Texas South Central", ring.srs.name)
                    ring.srs = str(SpatialReference(4326))  # back to WGS84
                    self.assertEqual(4326, ring.srs.srid)

                    # Using the `srid` property.
                    ring.srid = 4322
                    self.assertEqual("WGS 72", ring.srs.name)
                    self.assertEqual(4322, ring.srid)

            # srs/srid may be assigned their own values, even when srs is None.
            mpoly = OGRGeometry(mp.wkt, srs=None)
            mpoly.srs = mpoly.srs
            mpoly.srid = mpoly.srid
Beispiel #5
0
def pixel_value_from_point(raster, point, band=0):
    """
    Returns the pixel value for the coordinate of the input point from selected
    band.

    The input can be a point or tuple, if its a tuple it is assumed to be
    coordinates in the reference system of the raster.
    """
    if isinstance(point, (tuple, list)):
        point = OGRGeometry('POINT({0} {1})'.format(*point))
        point.srid = raster.srid
    elif not point.srs or not raster.srs:
        raise ValueError(
            'Both the point and the raster are required to have a reference system specified.'
        )
    elif point.srs != raster.srs:
        # Ensure the projection of the point is the same as of the raster.
        point.transform(raster.srid)

    # Return if point and raster do not touch.
    bbox = OGRGeometry.from_bbox(raster.extent)
    bbox.srs = raster.srs

    if not point.intersects(bbox):
        return

    # Compute position of point relative to raster origin.
    offset = (abs(raster.origin.x - point.coords[0]),
              abs(raster.origin.y - point.coords[1]))

    # Compute pixel index value based on offset.
    offset_index = [
        int(offset[0] / abs(raster.scale.x)),
        int(offset[1] / abs(raster.scale.y))
    ]

    # If the point is exactly on the boundary, the offset_index is rounded to
    # a pixel index over the edge of the pixel. The index needs to be reduced
    # by one pixel for those cases.
    if offset_index[0] == raster.width:
        offset_index[0] -= 1

    if offset_index[1] == raster.height:
        offset_index[1] -= 1

    return raster.bands[band].data(offset=offset_index, size=(1, 1))[0, 0]
Beispiel #6
0
def pixel_value_from_point(raster, point, band=0):
    """
    Returns the pixel value for the coordinate of the input point from selected
    band.

    The input can be a point or tuple, if its a tuple it is assumed to be
    coordinates in the reference system of the raster.
    """
    if isinstance(point, (tuple, list)):
        point = OGRGeometry('POINT({0} {1})'.format(*point))
        point.srid = raster.srid
    elif not point.srs or not raster.srs:
        raise ValueError('Both the point and the raster are required to have a reference system specified.')
    elif point.srs != raster.srs:
        # Ensure the projection of the point is the same as of the raster.
        point.transform(raster.srid)

    # Return if point and raster do not touch.
    bbox = OGRGeometry.from_bbox(raster.extent)
    bbox.srs = raster.srs

    if not point.intersects(bbox):
        return

    # Compute position of point relative to raster origin.
    offset = (abs(raster.origin.x - point.coords[0]), abs(raster.origin.y - point.coords[1]))

    # Compute pixel index value based on offset.
    offset_index = [int(offset[0] / abs(raster.scale.x)), int(offset[1] / abs(raster.scale.y))]

    # If the point is exactly on the boundary, the offset_index is rounded to
    # a pixel index over the edge of the pixel. The index needs to be reduced
    # by one pixel for those cases.
    if offset_index[0] == raster.width:
        offset_index[0] -= 1

    if offset_index[1] == raster.height:
        offset_index[1] -= 1

    return raster.bands[band].data(offset=offset_index, size=(1, 1))[0, 0]