scratch/docker/fedora-test/test.R

poly <- "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"foo\":\"bar\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[1745493.1964,6001802.1687],[1733165.0316,5989714.0167],[1709110.421,5986671.9829],[1701512.1629,5996204.8743],[1694071.9249,5996670.1668],[1698472.8948,5988332.4659],[1688395.3701,5989681.2008],[1677965.6958,6005588.9233],[1662101.242,6027765.2897],[1633099.9635,6066188.0936],[1632333.2593,6068917.4647],[1611887.8264,6107105.9235],[1615313.2862,6111814.5646],[1615273.0793,6122910.9659],[1607401.5736,6138887.1603],[1586362.9653,6168009.0013],[1580992.292,6187484.7528],[1598604.2695,6179319.1659],[1602302.8684,6165378.966],[1610993.2613,6147279.1175],[1618217.2473,6141247.7304],[1619611.4661,6131232.4434],[1626312.6186,6125451.5999],[1631049.39,6131536.119],[1635581.8515,6132044.6677],[1639612.9348,6128430.5315],[1648872.4808,6126997.4738],[1651390.585,6135510.2472],[1658793.6184,6130516.4984],[1664017.9086,6132002.6554],[1667504.0425,6122580.5619],[1684932.9873,6121315.2794],[1687775.4183,6113350.8992],[1698221.3987,6096136.1567],[1702333.8303,6093768.139],[1721686.2959,6084337.3811],[1733658.2241,6064257.0177],[1736021.5816,6054847.8425],[1734255.2674,6038727.4396],[1728574.9872,6038982.8492],[1731745.7286,6024545.9745],[1736045.2861,6011617.8379],[1745493.1964,6001802.1687]]]}}]}"

ctx <- V8::v8()
ctx$source(system.file("mapshaper/mapshaper-browserify.min.js",
                       package = "rmapshaper"))
ctx$assign("poly", poly)
ctx$eval("var return_data;")
ctx$eval("mapshaper.applyCommands(\"-simplify 0.05\", poly, function(Error, data) {
  if (Error) console.error('Error in V8 context: ' + Error.stack);
  return_data = data;
})")

ret_v8 <- ctx$get("return_data")

writeChar(poly, "poly.geojson")

system("mapshaper poly.geojson -simplify 0.05 -o poly_simp.geojson")
ret_sys <- readChar("poly_simp.geojson", nchars = 5000)

cat("original:")
print(nchar(poly))
cat("sys-false: ")
print(nchar(ret_v8))
cat("sys-true: ")
print(nchar(ret_sys))

all.equal(ret_v8, ret_sys)
ateucher/rmapshaper documentation built on July 8, 2024, 7:13 p.m.