Skip to content

Commit

Permalink
Add points along extent before reprojecting
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdhicks committed Oct 18, 2016
1 parent 26a81e5 commit ef538c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions datacube/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from datacube import compat
from datacube.utils import parse_time, cached_property, uri_to_local_path, check_intersect
from datacube.utils import schema_validated, DocReader, union_points, intersect_points
from datacube.utils import schema_validated, DocReader, union_points, intersect_points, densify_points

_LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -397,19 +397,23 @@ def from_sources_extents(cls, sources, geobox):

return cls(valid_data, geobox.crs)

def to_crs(self, crs):
def to_crs(self, crs, resolution=None):
"""
Duplicates polygon while transforming to a new CRS
:param CRS crs: Target CRS
:param resolution: resolution of points in source crs units to maintain in output polygon
:return: new GeoPolygon with CRS specified by crs
:rtype: GeoPolygon
"""
if self.crs == crs:
return self

if resolution is None:
resolution = 1 if self.crs.geographic else 100000

transform = osr.CoordinateTransformation(self.crs._crs, crs._crs) # pylint: disable=protected-access
return GeoPolygon([p[:2] for p in transform.TransformPoints(self.points)], crs)
return GeoPolygon([p[:2] for p in transform.TransformPoints(densify_points(self.points, resolution))], crs)

def __str__(self):
return "GeoPolygon(points=%s, crs=%s)" % (self.points, self.crs)
Expand Down
6 changes: 6 additions & 0 deletions datacube/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def _ogr_to_points(geom):
return geom.GetGeometryRef(0).GetPoints()[:-1]


def densify_points(points, resolution):
geom = _points_to_ogr(points)
geom.Segmentize(resolution)
return _ogr_to_points(geom)


def check_intersect(a, b):
assert a.crs == b.crs

Expand Down

0 comments on commit ef538c0

Please sign in to comment.