R Help

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

R Help

R-sig-geo mailing list
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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: R Help

Hugo Costa
Package rgeos is a good starting point.

Esu Esu via R-sig-Geo <[hidden email]> escreveu no dia quinta,
20/12/2018 à(s) 21:58:

> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] R Help

Rolf Turner
In reply to this post by R-sig-geo mailing list

On 12/21/18 10:56 AM, Esu Esu via R-sig-Geo wrote:

> Dear All,
> I am new to R-programming.

Learn to crawl before you start trying to walk or run.  Study some of
the many R tutorials that are available.

> 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

The spatstat package will enable you to do all this pretty easily.  See

     ?centroid.owin
     ?perimeter
     ?area.owin

You will need to convert your polygons to objects of class "owin".  The
as.owin() function should handle this for you.

cheers,

Rolf Turner


--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: R Help

Andres Diaz
In reply to this post by R-sig-geo mailing list
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 <[hidden email]>
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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Andrés D.

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: R Help

Mirza Cengic
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 <[hidden email]>
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 <
> [hidden email]>
> 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
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> --
> Andrés D.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> 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]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo