[R-sig-Geo] R Help

Mirza Cengic mirz@ceng @ending from gm@il@com
Fri Dec 21 14:07:14 CET 2018


Dear Esu,

you can probably solve these tasks using sf package, which uses bit
different way of representing spatial objects compared to the sp object.
Below you'll find an example for the North Carolina dataset. Keep in mind
that calculating area and perimeter depends on your coordinate system (i.e.
you can use projection that works better for calculating areas, or
distances; check the area/perimeter columns in nc dataset versus the output
of the functions below).
Package lwgeom is for the st_perimeter() function, and I converted it to
equidistant projection, since st_perimeter() doesn't like lat/long data.
Spatial objects of sp class you can convert to simple feature using
st_as_sf() function.

Hope it helps,
Mirza.

library(sf)
library(lwgeom)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc <- st_transform(nc, crs = "+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0
+y_0=0 +a=6371007 +b=6371007 +units=m +no_defs")

my_area <- st_area(nc)
my_centroid_x = st_coordinates(st_centroid(nc))[, 1]
my_centroid_y = st_coordinates(st_centroid(nc))[, 2]
my_perimeter = st_perimeter(nc)

On Fri, Dec 21, 2018 at 1:34 PM Andres Diaz Loaiza <madiazl using gmail.com>
wrote:

> Dear Esu,
>
> About your question there is huge literature already written.
>
> I recommend to read the sp package manual.
>
> An example for extract the coordinates you can  find it here:
>
>
> https://gis.stackexchange.com/questions/43543/how-to-calculate-polygon-centroids-in-r-for-non-contiguous-shapes
>
> If you want to see how works the function remember that r is an open source
> code and you can find the original functions over the binaries repositories
> or compressed packages.
>
> All the best,
>
> Andres
>
>
> On Thu, 20 Dec 2018 at 22:58, Esu Esu via R-sig-Geo <
> r-sig-geo using r-project.org>
> wrote:
>
> > Dear All,
> > I am new to R-programming. I have question if anyone can help me with:Q:
> > how can I create functions in R to compute the area (A), centroid (Cx and
> > Cy) and perimeter (P) of a polygon list .  I want to write R functions to
> > compute these quantities for individual polygons, as well asan overall
> > function to take a polygon list and return a data frame with four columns
> > and the samenumber of rows as polygons in the list.  Each quantity
> > should correspond to each column. it is (sp) class object.
> > Hope to find any suggestion or answer
> >  RegardsMuhsien
> >         [[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
> >
> --
> Andrés D.
>
>         [[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
>


-- 
*Mirza Čengić*

*Junior Researcher | Department of Environmental Science*
Radboud University | Heyendaalseweg 135
6525 AJ Nijmegen | The Netherlands

+31 657020823 | +38761908392
Skype: mirzacengic
<https://www.linkedin.com/in/mirzacengic>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list