Skip to content

Instantly share code, notes, and snippets.

@espinielli
Last active Oct 17, 2017
Embed
What would you like to do?
polygon area in R with sf
.Rproj.user
.Rhistory
.RData
.Ruserdata

Polygon are in R with sf

Enrico Spinielli
October 15, 2017

Source polygon_area.R and you will get the answer in ${km}^2$.

There are two CSV files for the polygons. Polygons are "closed", i.e. first and last point are the same.

The area of the full polygon is $22597969\ {km}^2$ while the area of the simplified one is $27412031\ {km}^2$.

Here is the map of the two (red = full polygon, blue = simple polygon).

---
title: "Polygon are in R with sf"
author: "Enrico Spinielli"
date: "October 15, 2017"
output:
html_document:
keep_md: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Source `polygon_area.R` and you will get the answer in ${km}^2$.
There are two CSV files for the polygons.
Polygons are "closed", i.e. first and last point are the same.
The area of the full polygon is $22597969\ {km}^2$
while the area of the simplified one is $27412031\ {km}^2$.
Here is the map of the two (red = full polygon, blue = simple polygon).
![](polys.png)
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
73.00000;0.00000
73.00000;0.00000
82.00000;0.00000
82.00000;32.00000
81.00000;32.00000
81.00000;35.00000
74.04500;35.00000
74.00000;32.08333
70.16667;32.08333
70.13333;31.98333
70.00000;31.13333
69.79472;30.81861
69.53333;30.83333
69.54167;30.56667
69.67222;30.13333
69.65833;30.08333
69.42194;30.02778
69.33333;29.53333
69.30833;29.49444
69.01667;29.05000
68.94083;28.57083
68.91667;28.41667
68.87500;28.80333
68.50333;28.45833
68.10000;29.33333
67.70000;30.00000
67.20389;29.40111
66.96667;29.08333
66.70000;29.61667
66.39722;29.75861
66.16667;29.86694
66.02194;29.95444
65.66667;30.16667
65.58361;29.81556
65.05639;29.62583
64.93333;29.61667
64.70000;30.08333
64.23333;30.50000
63.73333;30.01667
63.24111;31.17444
63.14667;31.26500
63.00000;31.50000
62.95000;31.55000
62.42167;31.10833
61.91944;30.27861
61.80000;30.08333
61.72167;29.94750
61.55111;29.65250
61.43333;29.45000
61.25333;29.11361
61.07583;28.75694
60.85833;28.33833
60.75000;28.16667
60.36528;27.67528
60.34500;27.64944
60.33167;27.62806
60.23333;27.47500
60.22333;27.45667
60.20028;27.29306
60.19389;27.22111
60.17167;26.94639
60.16139;26.83861
60.15250;26.75889
60.14972;26.71694
60.14417;26.66139
60.13333;26.55000
60.07611;26.39028
60.07028;26.37444
59.94556;26.03500
59.88333;25.86667
59.86667;25.97500
59.85167;26.07833
59.85000;26.08917
59.83750;26.17389
59.79639;26.44806
59.78944;26.49333
59.75528;26.72139
59.74694;26.77667
59.61167;27.63667
59.47167;28.04333
59.45667;28.09944
59.38611;28.19667
59.18639;27.88917
59.05139;27.77111
58.84333;27.46778
58.80028;27.38278
58.70000;27.41667
58.48861;27.49611
58.32944;27.51472
58.03833;27.62000
57.90000;27.81667
57.83000;27.55972
57.60000;27.38333
57.55000;27.70000
57.38028;27.68583
56.75167;27.90167
56.50500;28.12833
56.16667;28.15000
55.87417;30.01889
55.80000;30.48972
55.58333;30.94806
55.28000;30.86139
55.19167;30.96472
54.66833;31.04139
54.50333;31.08472
54.36500;31.12917
54.21667;31.33139
54.09472;31.49333
53.50278;32.27333
53.21667;32.46667
52.96833;31.35972
52.08139;31.95139
52.27500;32.67333
52.35000;33.31000
52.32167;33.89833
52.00000;34.11667
51.96667;34.16667
51.70000;34.50000
51.65000;34.08333
51.23333;34.45000
51.07167;35.33000
50.85000;35.43333
50.81583;35.44861
50.37167;35.64667
50.35500;35.69833
50.32139;36.29333
50.31667;36.40833
50.31333;36.48500
50.31667;36.82833
50.43333;37.46667
50.30167;37.64000
50.30083;37.64083
50.09194;37.76000
49.90861;38.04278
49.93028;38.14667
49.95000;38.17861
50.06611;38.19250
50.07500;38.22667
49.74167;39.50000
49.61250;40.15167
49.56306;40.20750
49.53194;40.07000
49.36667;40.16667
48.51833;39.86333
48.19500;39.94333
48.12833;39.89167
47.95000;39.80833
47.83333;39.67500
47.85667;39.21833
47.81667;38.91667
47.55500;38.31333
47.11167;38.22333
46.98333;38.33333
46.78333;37.50000
46.65833;37.42167
46.57333;37.36833
45.63833;36.79667
45.50444;36.71611
45.45000;36.68333
45.37833;36.68333
45.35000;36.64667
45.30667;36.59000
45.24500;36.59500
45.20500;36.53333
45.15917;36.54389
45.15333;36.54500
45.07167;36.57167
45.00000;36.59167
44.93667;36.61000
44.82917;36.62444
44.51667;36.66667
44.35000;36.63333
44.03667;36.55833
43.74167;36.49167
43.25000;36.38333
42.73222;36.26639
42.72500;36.50000
42.72000;36.61667
42.70889;36.91472
42.70333;37.12000
42.68333;37.66667
42.68222;37.71222
42.65333;37.80833
42.18389;39.40306
41.90000;40.33333
41.79917;40.65639
41.69750;40.97861
41.60000;41.28194
41.54889;41.44972
41.51667;41.54861
41.48361;41.65500
41.49750;41.70889
41.48972;41.72167
41.47500;41.71611
41.46417;41.78167
41.43472;41.82333
41.46083;41.87889
41.50000;41.90778
41.52444;41.96556
41.52528;41.97694
41.50639;41.99250
41.49417;42.04833
41.51167;42.08694
41.51417;42.18389
41.49333;42.20417
41.49389;42.28889
41.47111;42.33389
41.46639;42.40778
41.44278;42.43917
41.43917;42.47083
41.44000;42.52056
41.46833;42.51056
41.51056;42.56972
41.56167;42.57806
41.57583;42.59611
41.57833;42.78389
41.57083;42.83861
41.51667;42.79694
41.42806;42.97889
41.30333;43.19889
41.25333;43.12528
41.24694;43.19778
41.17611;43.22722
41.20222;43.37417
41.17944;43.43778
41.13333;43.46667
41.09194;43.44972
41.05972;43.47083
41.02750;43.47194
40.98556;43.59944
40.93306;43.67139
40.84444;43.67611
40.84000;43.68500
40.81194;43.71333
40.73611;43.74889
40.70778;43.73639
40.68139;43.74917
40.58750;43.68694
40.56556;43.64972
40.56250;43.64694
40.52750;43.64694
40.53667;43.63111
40.52083;43.63528
40.52694;43.62417
40.50639;43.58333
40.47750;43.54750
40.45694;43.55639
40.41806;43.62083
40.38556;43.60972
40.34528;43.59250
40.27111;43.63917
40.25694;43.68167
40.22444;43.68222
40.22083;43.65889
40.16417;43.71389
40.14833;43.66139
40.11556;43.66472
40.07972;43.71889
40.07944;43.76972
40.01833;43.90556
40.03083;44.11250
40.01972;44.17139
40.03500;44.19806
40.04528;44.27889
40.03167;44.29889
40.02861;44.33972
40.00000;44.37556
40.00000;44.41917
39.96444;44.48861
39.90361;44.55750
39.87722;44.56444
39.85056;44.59694
39.82556;44.60167
39.38333;44.45000
39.32333;44.08667
38.90972;44.20833
38.32222;44.50417
38.04889;44.29972
37.89722;44.23611
37.70833;44.62667
37.10833;44.66667
36.94167;44.26667
37.30556;44.16667
37.22472;43.64056
37.35000;43.21667
37.37639;43.11833
37.30556;42.82361
37.24889;42.64972
37.08333;42.33333
37.26667;42.25000
37.10333;41.79111
37.00000;41.50000
37.07222;41.23028
37.03333;41.19861
37.03333;40.83333
36.66667;39.33333
36.70861;39.02833
36.66667;38.75000
36.90000;38.36667
36.64889;37.38306
36.65000;37.06389
36.83333;36.66667
36.78444;36.64139
36.50000;36.58750
36.23333;36.70000
36.16667;36.36667
35.86667;36.18333
35.88333;36.03333
35.91667;35.66667
35.55000;35.53333
35.19333;35.71667
34.58333;35.75000
34.53833;35.28667
34.48333;35.05000
34.36667;34.87444
34.24500;34.74667
34.03167;34.62139
33.94556;34.60389
33.89167;34.59167
33.83194;34.58333
33.64889;34.63306
33.16667;34.93333
33.10000;34.86667
33.10000;34.71667
33.09083;34.60306
32.85750;34.55361
32.78167;34.54389
32.71667;34.53333
32.68056;34.42750
32.61389;34.35444
32.50583;34.18806
32.49472;34.17083
32.33500;34.05528
32.16944;33.99250
31.91667;33.98333
31.83333;34.00000
31.82500;34.00194
32.21667;33.10000
32.63333;32.08333
32.80861;31.66194
33.10000;30.95000
33.50000;30.00000
33.71389;28.75000
33.80000;28.26667
33.92389;27.62306
33.95167;27.33333
34.00000;27.16667
34.00000;24.16667
34.33333;23.58333
34.33333;11.50000
37.50000;11.50000
37.59278;11.29417
37.64167;11.20000
37.67361;11.11389
37.68333;11.10000
37.74278;10.95917
37.76944;10.90861
37.81944;10.78667
37.88917;10.62861
38.03889;10.28639
38.25472;9.78722
38.39528;9.45639
38.58333;9.01667
38.60306;8.96500
38.63306;8.89222
38.64444;8.86528
38.65750;8.83667
38.68417;8.77194
38.79500;8.49944
39.00000;8.00000
39.00000;4.68500
38.48028;3.94972
38.33333;3.75000
38.26056;3.55000
38.21333;3.42194
38.13194;3.20194
38.11306;3.15139
37.90250;2.58944
37.80361;2.32889
37.71667;2.12500
37.44167;1.50000
37.27472;1.12389
36.91667;0.10000
36.87111;-0.02361
36.45361;-1.02361
36.40500;-1.13778
36.25222;-1.49500
35.88333;-2.02500
35.83333;-2.00000
35.83333;-7.38333
35.96667;-7.38333
35.96667;-12.00000
35.54639;-12.31250
35.00000;-12.71167
34.00000;-13.42722
33.19028;-13.97972
33.00000;-14.12333
32.25000;-14.61667
32.00000;-14.80111
31.50000;-15.75000
31.42139;-16.03278
31.40694;-16.55694
31.45000;-16.80000
31.48306;-16.94056
31.50250;-16.99500
31.65000;-17.41667
31.68306;-17.45694
31.83333;-17.71667
32.33111;-18.13028
32.98333;-18.40000
33.92500;-18.06917
34.16667;-17.80000
35.52056;-16.23444
36.19444;-15.39028
36.50000;-15.00000
37.00000;-15.00000
38.00000;-15.00000
38.50000;-15.00000
39.00000;-15.00000
39.50000;-15.00000
40.00000;-15.00000
41.00000;-15.00000
42.00000;-15.00000
42.50000;-14.00000
43.00000;-13.00000
45.00000;-13.00000
45.00000;-8.75000
48.57333;-8.75000
49.00000;-15.00000
57.00000;-15.00000
57.00000;-10.00000
61.00000;-10.00000
61.00000;0.00000
73.00000;0.00000
# install.packages(c("tidyverse", "sf", "units", "mapview")
library(tidyverse)
library(sf)
library(units)
library(mapview)
# list of point defining the polygon (last point equal to first one)
poly_full_csv <- read_delim(
"full_polygon.csv",
delim = ";",
col_names = c("lat", "lon"),
col_types = "dd")
# from list of points to SF polygon
poly_full <- poly_full_csv %>%
select(lon, lat) %>%
as.matrix() %>%
list() %>%
st_polygon()
poly_simple_csv <- read_delim(
"simple_polygon.csv",
delim = ";",
col_types = "dd", col_names = FALSE
) %>% rename(lon = X2, lat = X1)
poly_simple <- poly_simple_csv %>%
select(lon, lat) %>%
as.matrix() %>%
list() %>%
st_polygon()
ps <- st_sf(a = 1, geometry = st_sfc(poly_simple)) %>%
st_set_crs(4326)
pf <- st_sf(a = 1, st_sfc(poly_full)) %>%
st_set_crs(4326)
pf %>% st_area() %>% set_units(km^2)
ps %>% st_area() %>% set_units(km^2)
mapview(pf, color = "white", col.regions = "red") + mapview(ps)
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: knitr
LaTeX: pdfLaTeX
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
81.07095264;-1.249340377
82.15928065;20.49622637
79.98098991;37.36859498
36.99425199;44.74575178
33.9395891;25.52224348
35.61426157;-6.585536566
36.32646554;-12.62765374
39.68569469;-15.69204363
81.07095264;-1.249340377
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment