Convert XY to lat long in R

2.6k Views Asked by At

I am trying to plot the position of ports on a leaflet map which fall into a certain region. These 30 odd regions have been created using the mapedit package as sf POLYGONs. The code for one such region WestCoastIndiax(West coast of India) is as follows:

structure(list(X_leaflet_id = 629L, feature_type = "polygon", 
    geometry = structure(list(structure(list(structure(c(-298.1964, 
    -298.4601, -298.3722, -297.6855, -293.9062, -293.4009, -292.8296, 
    -292.1265, -291.7529, -289.3359, -288.1934, -289.248, -289.0723, 
    -287.7539, -284.8535, -286.04, -283.0957, -282.3157, -282.4255, 
    -282.9529, -283.4253, -284.0295, -284.9634, -285.8643, -286.7651, 
    -286.7432, -286.875, -286.9519, -288.2922, -288.6328, -290.05, 
    -291.8958, -292.8955, -293.4009, -294.6863, -298.1964, 25.6861, 
    24.8565, 24.682, 24.8765, 25.1453, 24.5671, 23.8256, 23.5237, 
    22.6343, 19.6012, 20.0559, 12.5975, 7.1881, -1.1425, -0.791, 
    8.4941, 8.0484, 7.8416, 8.2441, 9.2973, 10.4338, 11.62, 14.094, 
    16.1197, 19.3215, 20.0456, 21.8411, 22.6748, 23.433, 24.4572, 
    24.7768, 25.0657, 25.3043, 25.8592, 26.116, 25.6861), .Dim = c(36L, 
    2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
    "sfc"), precision = 0, bbox = structure(c(xmin = -298.4601, 
    ymin = -1.1425, xmax = -282.3157, ymax = 26.116), class = "bbox"), crs = structure(list(
        epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, sf_column = "geometry", agr = structure(c(X_leaflet_id = NA_integer_, 
feature_type = NA_integer_), class = "factor", .Label = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame"))

The region plots correctly in leaflet.

leaflet() %>% addTiles() %>% addPolygons(data=WestCoastIndiax)

I am then trying to see whether a particular location falls within these regions by using the st_within() function from the sf package. An example for the city of Mumbai, which does fall in this region, is:

st_within(st_point(c(72.49,18.54)),WestCoastIndiax)

which returns:

Sparse geometry binary predicate list of length 1, where the predicate was `within'
 1: (empty)

When I try to plot the polygon and the coordinates of Mumbai, this is what I get:

leaflet() %>% addTiles() %>% addPolygons(data=WestCoastIndiax) %>% addMarkers(lng = 72.49, lat = 18.54)

enter image description here

I had a look at this and gathered that I need to have the correct crs and then transform it again to 4326. However, I don't know what the coordinate reference system should be for these 30 odd geometries. I tried this:

st_crs(WestCoastIndiax) <- "+init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
WestCoastIndiax <- st_transform(WestCoastIndiax, 4326)

And while now the bbox units changed, but they are not correct:

Geometry set for 1 feature 
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -0.002681113 ymin: -1.026325e-05 xmax: -0.002536085 ymax: 0.000234604
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
POLYGON ((-0.002678744 0.0002307422, -0.0026811...

Would appreciate some help here please.

1

There are 1 best solutions below

0
On

Your data is in crs 4326 alright! What happened is that very likely when creating the polygon with mapedit, the features were drawn in the wrong place (i.e. outside -180/180 longitude). You can check this by substracting 360° from your point location which then lies within your polygon.

mapview::mapview(tst) + sf::st_point(c(72.49-360,18.54))

Hence, you need to either update your x-coordinates by adding 360° or re-draw the polygon in the correct position. You can update the coordinates of your polygon as follows:

st_geometry(WestCoastIndiax) = st_geometry(WestCoastIndiax) + c(360, 0)
st_crs(WestCoastIndiax) = 4326

Note that we loose the crs info when adding the offsets, so we need to redefine the crs.

I have openend an issue/feature request in mapedit to make sure drawing is only allowed within sane longlat bounds.