Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Created August 2, 2017 22:20
Show Gist options
  • Save valentinitnelav/bdea511468eed337440ab2f2bc12c000 to your computer and use it in GitHub Desktop.
Save valentinitnelav/bdea511468eed337440ab2f2bc12c000 to your computer and use it in GitHub Desktop.

Revisions

  1. valentinitnelav created this gist Aug 2, 2017.
    22 changes: 22 additions & 0 deletions Euclidean_buffer_distortions.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    require(rgeos)
    require(sp)
    require(plotKML)

    # Generate a random grid-points for a (almost) global bounding box
    b.box <- as(raster::extent(120, -120, -60, 60), "SpatialPolygons")
    proj4string(b.box) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
    set.seed(2017)
    pts <- sp::spsample(b.box, n=100, type="regular")
    plot(pts@coords)

    # Project to Mollweide projection to be able to apply buffer with `gBuffer`
    # (one could use other projection)
    pts.moll <- sp::spTransform(pts, CRSobj = "+proj=moll")
    # create 1000 km buffers around the points
    buf1000km.moll <- rgeos::gBuffer(spgeom = pts.moll, byid = TRUE, width = 10^6)
    plot(buf1000km.moll)
    # convert back to WGS84 unprojected
    buf1000km.WGS84 <- sp::spTransform(buf1000km.moll, CRSobj = proj4string(pts))
    plot(buf1000km.WGS84) # distorsions are present
    # save as KML to visualize distorted Euclidian buffers on Google Earth
    plotKML::kml(buf1000km.WGS84, file.name = "buf1000km.WGS84.kml")