[R-sig-Geo] convert from any spatial* classes to a specific character string

Robin Lovelace rob00x @end|ng |rom gm@||@com
Wed Sep 2 17:25:13 CEST 2020


Does this help? The approach covers sf, sp and maybe other classes that can
be converted into sf objects.

``` r
library(spData)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0
x_sf = rmapshaper::ms_simplify(lnd[1:3, ], 0.01)
#> Registered S3 method overwritten by 'geojsonlint':
#>   method         from
#>   print.location dplyr
x_sp = sf::as_Spatial(x_sf)
geo_to_char = function(x) {
  if(!is(object = x, class2 = "sf")) {
    x = sf::st_as_sf(x)
  }
  sf::st_as_text(sf::st_geometry(x))
}
geo_to_char(x_sf)
#> [1] "POLYGON ((-0.3306791 51.32901, -0.3305339 51.34842, -0.3086951
51.37545, -0.3177201 51.39367, -0.3096538 51.40001, -0.3060304 51.42123,
-0.2866189 51.42017, -0.2540906 51.43729, -0.2446848 51.40423, -0.2474248
51.39758, -0.2387446 51.38609, -0.2608406 51.37956, -0.2854616 51.36425,
-0.3039032 51.34325, -0.3057101 51.33541, -0.3193073 51.32781, -0.3306791
51.32901))"



#> [2] "POLYGON ((-0.0785486 51.41985, -0.0689427 51.40418, -0.0427724
51.38945, -0.0369519 51.37701, -0.0053322 51.35268, 0.0022661 51.32914,
-0.0251301 51.33861, -0.0379184 51.33871, -0.0478549 51.32651, -0.0641537
51.31863, -0.0848293 51.31587, -0.0943518 51.29936, -0.1243197 51.28676,
-0.1373403 51.30078, -0.155344 51.30128, -0.1619055 51.31963, -0.1444548
51.32648, -0.1475475 51.33878, -0.1275566 51.34745, -0.1169191 51.34575,
-0.1233396 51.37119, -0.1337986 51.39127, -0.1245223 51.39741, -0.1324811
51.40841, -0.1126856 51.42324, -0.0785486 51.41985))"

#> [3] "POLYGON ((0.0022661 51.32914, -0.0053322 51.35268, -0.0369519
51.37701, -0.0427724 51.38945, -0.0689427 51.40418, -0.0785486 51.41985,
-0.0748669 51.4258, -0.0436909 51.42291, -0.0301069 51.42565, -0.0105455
51.41355, 0.0254105 51.42899, 0.0398142 51.44099, 0.0585966 51.42459,
0.0753431 51.43199, 0.1124939 51.41317, 0.134136 51.41385, 0.1488766
51.40848, 0.1598909 51.39465, 0.1498092 51.39087, 0.1532098 51.37804,
0.1362526 51.34555, 0.1166538 51.3413, 0.1202182 51.33214, 0.1047428
51.32734, 0.0850008 51.31602, 0.0856654 51.29309, 0.0584828 51.28936,
0.0457041 51.29401, 0.0328814 51.30752, 0.0149821 51.29179, 0.0022661
51.32914))"
geo_to_char(x_sp)
#> [1] "POLYGON ((-0.3306791 51.32901, -0.3305339 51.34842, -0.3086951
51.37545, -0.3177201 51.39367, -0.3096538 51.40001, -0.3060304 51.42123,
-0.2866189 51.42017, -0.2540906 51.43729, -0.2446848 51.40423, -0.2474248
51.39758, -0.2387446 51.38609, -0.2608406 51.37956, -0.2854616 51.36425,
-0.3039032 51.34325, -0.3057101 51.33541, -0.3193073 51.32781, -0.3306791
51.32901))"



#> [2] "POLYGON ((-0.0785486 51.41985, -0.0689427 51.40418, -0.0427724
51.38945, -0.0369519 51.37701, -0.0053322 51.35268, 0.0022661 51.32914,
-0.0251301 51.33861, -0.0379184 51.33871, -0.0478549 51.32651, -0.0641537
51.31863, -0.0848293 51.31587, -0.0943518 51.29936, -0.1243197 51.28676,
-0.1373403 51.30078, -0.155344 51.30128, -0.1619055 51.31963, -0.1444548
51.32648, -0.1475475 51.33878, -0.1275566 51.34745, -0.1169191 51.34575,
-0.1233396 51.37119, -0.1337986 51.39127, -0.1245223 51.39741, -0.1324811
51.40841, -0.1126856 51.42324, -0.0785486 51.41985))"

#> [3] "POLYGON ((0.0022661 51.32914, -0.0053322 51.35268, -0.0369519
51.37701, -0.0427724 51.38945, -0.0689427 51.40418, -0.0785486 51.41985,
-0.0748669 51.4258, -0.0436909 51.42291, -0.0301069 51.42565, -0.0105455
51.41355, 0.0254105 51.42899, 0.0398142 51.44099, 0.0585966 51.42459,
0.0753431 51.43199, 0.1124939 51.41317, 0.134136 51.41385, 0.1488766
51.40848, 0.1598909 51.39465, 0.1498092 51.39087, 0.1532098 51.37804,
0.1362526 51.34555, 0.1166538 51.3413, 0.1202182 51.33214, 0.1047428
51.32734, 0.0850008 51.31602, 0.0856654 51.29309, 0.0584828 51.28936,
0.0457041 51.29401, 0.0328814 51.30752, 0.0149821 51.29179, 0.0022661
51.32914))"
```

<sup>Created on 2020-09-02 by the [reprex package](
https://reprex.tidyverse.org) (v0.3.0)</sup>

On Wed, Sep 2, 2020 at 1:25 PM Matteo Mattiuzzi <matteo using mattiuzzi.com>
wrote:

> Dear list,
>
> I have recently submitted a little R package to CRAN (now waiting for
> approval) https://github.com/MatMatt/clmsapi.
> The package is a R client for downloading data from the Copernicus Land
> Monitoring Service (https://cryo.land.copernicus.eu/finder/).
>
> The API expects a parameter called 'geometry' with the following formatting
> (character):
> 'POLYGON((lon+lat,lon+lat,lon+lat, ... ))' # commas are replaced with ascii
> '%2C' in the query string
> or
> 'POINT(lon+lat)'
> see also here:
> https://cryo.land.copernicus.eu/resto/api/collections/HRSI/describe.xml
>
> I could expect users to prepare the geometry parameter as it is needed by
> the API, but I would prefer to improve the user friendliness by adding some
> functionalities (similar as in the MODIS package) that can convert from
> different spatial extent objects/classes to the needed character string (in
> clmsapi::composeUrl).
>
> What is the best way to implement that? What are the classes I should
> consider as input for the geometry parameter? (i.e. well established and
> well supplied with conversion-functions).
>
> Thanks and kind regards, Matteo
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list