Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproject and crop are not commutative #135

Open
jmarintur opened this issue Aug 24, 2018 · 6 comments
Open

Reproject and crop are not commutative #135

jmarintur opened this issue Aug 24, 2018 · 6 comments
Labels
bug Something isn't working

Comments

@jmarintur
Copy link

In the first case I perform crop and then reproject:

  1. raster.crop(roi.envelope).reproject(dst_crs=WEB_MERCATOR_CRS).save(output_raster)

In the second case I reproject and then I crop:

  1. raster.reproject(dst_crs=WEB_MERCATOR_CRS).crop(roi.envelope).save(output_raster)

The resulting images when I visualize them using Qgis 2.14.11-Essen look like this:

  1. reproject_later

And

  1. reproject_before

I am using a Sentinel-2 image Band 02 and tile T18HXB and the following roi:

coord = [-8083749.056641963, -4737939.582734095, -8079749.056641963, -4733939.582734095]
roi = tl.GeoVector.from_bounds(xmin=coordindates[0], ymin=coordindates[1], xmax=coordindates[2], ymax=coordindates[3], crs=WEB_MERCATOR_CRS)

Thanks for your support.

@astrojuanlu
Copy link
Contributor

Thanks a lot for the good bug report @jmarintur! Could you please give the output of crs.raster for completeness?

@astrojuanlu astrojuanlu added the bug Something isn't working label Aug 24, 2018
@jmarintur
Copy link
Author

This is it: CRS({'init': 'epsg:32718'})

Thanks @Juanlu001 !

@drnextgis
Copy link
Contributor

drnextgis commented Aug 27, 2018

It is expected that shapes in these cases are different (at least from code perspective). Let's see what is happening.

First case (crop first):

1 roi.envelope.get_bounds(self.crs) is called inside crop here
2 roi.envelope in original CRS epsg:3857 is not the same as bounds of the roi.envelope in epsg:32718
3 image after cropping in epsg:32718 and result image reprojected to epsg:3857, red polygon - roi.envelope:
image
image

Result image in the second case (reproject first):

image

@drnextgis
Copy link
Contributor

drnextgis commented Aug 27, 2018

If reprojecting first is not a solution you can use mask method of GeoRaster:

buffer = GeoVector(roi.get_shape(WEB_MERCATOR_CRS).buffer(100), WEB_MERCATOR_CRS)
raster.crop(buffer).reproject(dst_crs=WEB_MERCATOR_CRS).mask(roi).save(output_raster)

Example of output:

image

@astrojuanlu
Copy link
Contributor

Thanks @drnextgis! I have a couple of questions:

  1. I understand that crop and reproject are not meant to be commutative. Reprojecting a big image can be extremely expensive, especially when in the end you only want one crop. The buffer solution is nice, but a bit manual. Do you think it's worth it that telluric includes such a function?
  2. Why mask instead of crop as the last step?

@drnextgis
Copy link
Contributor

drnextgis commented Aug 28, 2018

  1. I think that it would be handy to have crop parameter for mask: raster.mask(vector, crop=True)
  2. Let's see what happening in case of using mask (first image) and crop (second one) as the last step:
    image
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants