extract lat-long pairs from sf::st_read MULTIPOLYGON format

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

extract lat-long pairs from sf::st_read MULTIPOLYGON format

Brandon Payne

## I am trying to modify the

install_github("choroplethrAdmin1", "arilamstein")

package.  It can show a map by

choroplethrAdmin1::admin1_map("israel")

The shape data is hidden in .Rdata, (principle of encapsulation)
so to get at it I had to

```{bash}
git clone [hidden email]:arilamstein/choroplethrAdmin1.git
cd chor*
```
```{r}
load("/Users/AbuDavid/scratch/R/choroplethrAdmin1/data/admin1.regions.rdata")
ilmap<-admin1.map[which(admin1.map$admin == name), ]
View(ilmap)
#write.csv(ilmap, file = "israel.csv", row.names=FALSE)

```

the format of this data looks like

35.13070357 32.70585928 israel haifa district 1879.1 272218 FALSE 1 1879
35.12837813 32.70314627 israel haifa district 1879.1 272219 FALSE 1 1879
35.12574263 32.7008725 israel haifa district 1879.1 272220 FALSE 1 1879
35.12419234 32.69989065 israel haifa district 1879.1 272221 FALSE 1 1879
35.12248701 32.69919302 israel haifa district 1879.1 272222 FALSE 1 1879
35.12078169 32.69911551 israel haifa district 1879.1 272223 FALSE 1 1879
35.02130456 32.38117626 israel central district 1880.1 272295 FALSE 1 1880
35.0327303 32.38220026 israel central district 1880.1 272296 FALSE 1 1880
35.02879684 32.36515737 israel central district 1880.1 272297 FALSE 1 1880
35.02156213 32.34459015 israel central district 1880.1 272298 FALSE 1 1880
35.01784143 32.34221303 israel central district 1880.1 272299 FALSE
1 1880

## Question:

How can I get the data from my shape file as 2 columns: c(lat, long) ?

I don't mind using something other than sf:st_read.

```{r importshapes}
editedmap <- sf::st_read("../includes/ISR_adm_edited/ISR_adm1.shp")
golan <- editedmap[which(editedmap$NAME_1 == "Golan"), ]
names(editedmap)
```

> names(editedmap)
 [1] "ID_0"      "ISO"       "NAME_0"    "ID_1"      "NAME_1"    "TYPE_1"  

 [7] "ENGTYPE_1" "NL_NAME_1" "VARNAME_1" "geometry"

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

Re: extract lat-long pairs from sf::st_read MULTIPOLYGON format

Roman Luštrik
After downloading
https://github.com/trulia/choroplethrAdmin1/blob/master/data/admin1.map.rdata
and

load("admin1.map.rdata")
ilmap<-admin1.map[which(admin1.map$admin == "slovenia"), ]


I get a data.frame

> names(ilmap)
 [1] "long"       "lat"        "order"      "hole"       "piece"
 "group"
 [7] "id"         "adm1_code"  "OBJECTID_1" "diss_me"    "adm1_cod_1"
"iso_3166_2"
[13] "wikipedia"  "iso_a2"     "adm0_sr"    "name"       "name_alt"
"name_local"
[19] "type"       "type_en"    "code_local" "code_hasc"  "note"
"hasc_maybe"
[25] "region"     "region_cod" "provnum_ne" "gadm_level" "check_me"
"scalerank"
[31] "datarank"   "abbrev"     "postal"     "area_sqkm"  "sameascity"
"labelrank"
[37] "featurecla" "name_len"   "mapcolor9"  "mapcolor13" "fips"
"fips_alt"
[43] "woe_id"     "woe_label"  "woe_name"   "latitude"   "longitude"
 "sov_a3"
[49] "adm0_a3"    "adm0_label" "admin"      "geonunit"   "gu_a3"
 "gn_id"
[55] "gn_name"    "gns_id"     "gns_name"   "gn_level"   "gn_region"
 "gn_a1_code"
[61] "region_sub" "sub_code"   "gns_level"  "gns_lang"   "gns_adm1"
"gns_region"


Notice the first two columns. If this isn't what you're after, perhaps try
coordinates().

Cheers,
Roman


On Sat, May 6, 2017 at 10:56 PM, Brandon Payne <[hidden email]>
wrote:

>
> ## I am trying to modify the
>
> install_github("choroplethrAdmin1", "arilamstein")
>
> package.  It can show a map by
>
> choroplethrAdmin1::admin1_map("israel")
>
> The shape data is hidden in .Rdata, (principle of encapsulation)
> so to get at it I had to
>
> ```{bash}
> git clone [hidden email]:arilamstein/choroplethrAdmin1.git
> cd chor*
> ```
> ```{r}
> load("/Users/AbuDavid/scratch/R/choroplethrAdmin1/data/
> admin1.regions.rdata")
> ilmap<-admin1.map[which(admin1.map$admin == name), ]
> View(ilmap)
> #write.csv(ilmap, file = "israel.csv", row.names=FALSE)
>
> ```
>
> the format of this data looks like
>
> 35.13070357     32.70585928     israel  haifa district  1879.1  272218
> FALSE   1       1879
> 35.12837813     32.70314627     israel  haifa district  1879.1  272219
> FALSE   1       1879
> 35.12574263     32.7008725      israel  haifa district  1879.1  272220
> FALSE   1       1879
> 35.12419234     32.69989065     israel  haifa district  1879.1  272221
> FALSE   1       1879
> 35.12248701     32.69919302     israel  haifa district  1879.1  272222
> FALSE   1       1879
> 35.12078169     32.69911551     israel  haifa district  1879.1  272223
> FALSE   1       1879
> 35.02130456     32.38117626     israel  central district        1880.1
> 272295  FALSE   1       1880
> 35.0327303      32.38220026     israel  central district        1880.1
> 272296  FALSE   1       1880
> 35.02879684     32.36515737     israel  central district        1880.1
> 272297  FALSE   1       1880
> 35.02156213     32.34459015     israel  central district        1880.1
> 272298  FALSE   1       1880
> 35.01784143     32.34221303     israel  central district        1880.1
> 272299  FALSE
> 1       1880
>
> ## Question:
>
> How can I get the data from my shape file as 2 columns: c(lat, long) ?
>
> I don't mind using something other than sf:st_read.
>
> ```{r importshapes}
> editedmap <- sf::st_read("../includes/ISR_adm_edited/ISR_adm1.shp")
> golan <- editedmap[which(editedmap$NAME_1 == "Golan"), ]
> names(editedmap)
> ```
>
> > names(editedmap)
>  [1] "ID_0"      "ISO"       "NAME_0"    "ID_1"      "NAME_1"    "TYPE_1"
>
>  [7] "ENGTYPE_1" "NL_NAME_1" "VARNAME_1" "geometry"
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



--
In God we trust, all others bring data.

        [[alternative HTML version deleted]]

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

Re: extract lat-long pairs from sf::st_read MULTIPOLYGON format

Brandon Payne

Roman,

I am looking for advice on how to convert one (extra, missing, not-included) region from the shape file
"ISR_adm1.shp" @ "http://biogeo.ucdavis.edu/data/diva/adm/ISR_adm.zip",

into that ( https://github.com/trulia/choroplethrAdmin1/blob/master/data/admin1.map.rdata ) data frame format.

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

Re: extract lat-long pairs from sf::st_read MULTIPOLYGON format

Tim Appelhans
Brandon,

sf has sf::st_coordinates which gives a matrix or list of matrices of X
and Y coordinates depending on the feature type.

Tim


On 07.05.2017 10:17, Brandon Payne wrote:

> Roman,
>
> I am looking for advice on how to convert one (extra, missing, not-included) region from the shape file
> "ISR_adm1.shp" @ "http://biogeo.ucdavis.edu/data/diva/adm/ISR_adm.zip",
>
> into that ( https://github.com/trulia/choroplethrAdmin1/blob/master/data/admin1.map.rdata ) data frame format.
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

--
Tim Appelhans
Research Specialist, Geomarketing
GfK | Bamberger Str. 6, 90425 Nürnberg | Germany
Postal address: Nordwestring 101 | 90419 Nürnberg

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

Re: extract lat-long pairs - slot "coords"

Brandon Payne
In reply to this post by Roman Luštrik

I was able to get the coords to print to the R-console.  Still need to
read the docs on the Polygon class to access them.
(It's not  Golan$coords   )

```{pseudo}
# download
"http://biogeo.ucdavis.edu/data/diva/adm/ISR_adm.zip",
# extract
unzip *.zip
```

```{r}
states <- readOGR("../includes/ISR_adm/ISR_adm1.shp", "ISR_adm1")
Golan <- subset(states, NAME_1 == "Golan")
## check
## based on http://stackoverflow.com/questions/25985395/how-to-subset-a-shapefile
dim(states)
dim(Golan)
Golan

```
>
An object of class "Polygon"
Slot "labpt":
[1] 35.64728 33.24579

Slot "area":
[1] 6.914819e-05

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
          [,1]     [,2]
 [1,] 35.63564 33.24520
 [2,] 35.63712 33.24554
 [3,] 35.64099 33.24652
 [4,] 35.64528 33.24749
 [5,] 35.65008 33.24818
 [6,] 35.65404 33.24831
 [7,] 35.65889 33.24823
 [8,] 35.65992 33.24816
 [9,] 35.65163 33.24443
[10,] 35.64420 33.24282
[11,] 35.63676 33.24432
[12,] 35.63564 33.24520

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