I hope to plot SpatialPolygons on satellite raster images. The coordinates of SpatialPolygons are converted to be in the range of x = [0,1] and y = [-1,0]. I know that this overlay plot can be done in python. I am curious if we can do this in R as well?


> print(readWKT(wkt))
class       : SpatialPolygons 
features    : 1 
extent      : 0, 0.009158, -0.009045, 0  (xmin, xmax, ymin, ymax)
coord. ref. : NA 

> print(raster("three_band/6040_2_2.tif"))
class       : RasterLayer 
band        : 1  (of  3  bands)
dimensions  : 3349, 3391, 11356459  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : 0, 3391, 0, 3349  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : three_band\6040_2_2.tif 
names       : X6040_2_2 
values      : 0, 65535  (min, max)
  • Why not plot in the geographic space of the image? Is it because they are curvilinear? (It's common in remote sensing and model outputs) In that case I convert the coordinates of the SpatialPolygons into the index space of the raw grid by local transformation - not just global scaling - there aren't native tools to do this but it's easy enough to understand, and it works for all sorts of applications beyond vis. – mdsumner Dec 20 '16 at 1:46
  • @mdsumner thanks for your reply! Could you please provide some specific examples? I have plotted them side by side. – Randel Dec 20 '16 at 2:26
  • Can you print one instance of the data and post here? Just print(imgs) and print(readWKT(I)) - but better if you can share a WKT string and one of your IMG files – mdsumner Dec 20 '16 at 9:35
  • I meant "one of your tif files" (not "IMG"). I suspect you need to georeference the image, which is exactly the opposite of what you asked. But please share the metadata details - via print(). (You may only need to spTransform() the polygons to the raster). – mdsumner Dec 20 '16 at 9:54
  • @mdsumner thanks! I updated the question with the printed information. I guess we need to make the two extents comparable. Please let me know if you need more information. – Randel Dec 20 '16 at 17:20

You can use the elide function from the maptools package to shift and scale SpatialPolygons objects. For your use case you'll have to compute the shift and scale based on the bounding box of your (unstated) object, but its just a bit of trivial linear algebra. Documentation extract follows, for the full docs, install maptools and read the help.

      shift values to shift the coordinates of the input object;
          this is made ineffective by the scale argument

      scale if NULL, coordinates not scaled; if TRUE, the longer
          dimension is scaled to lie within [0,1] and aspect
          maintained; if a scalar, the output range of [0,1] is
          multiplied by scale
  • Thanks a lot for your prompt reply! I am wondering if it is possible to scale longitude and latitude with different scales. – Randel Dec 19 '16 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.