noob question: access single region from .shp file

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

noob question: access single region from .shp file

Brandon Payne
How do you access a single region in a shape file?

<begin pseudo code:>
if (Israelshape2$NAME_1=="Golan") {
Color= "Red"}
</end>

<begin real code:>
#! /usr/bin/R

## grab shape file
download.file("http://biogeo.ucdavis.edu/data/diva/adm/ISR_adm.zip",
              "./includes/ISR_adm.zip")
             
unzip("./includes/ISR_adm.zip", files=Sys.glob(".shp"),
      unzip="internal",       exdir = "./shapeFiles")

## libraries
library(maptools)
library(RColorBrewer)
colors <- brewer.pal(9, "BuGn")
library(ggmap)

## google maps
mapImageData3 <- get_map(location = c(lon = 35.1660235,
                                      lat = 31.32226),
                         color="color",
                         source = "google",
                         maptype = "satellite",
                         zoom = 7)

## read shape file
IsraelPolygon <- readShapePoly("./includes/ISR_adm/ISR_adm1.shp")
IsraelPoints  <- fortify(IsraelPolygon)

## plot shapes on top of map, output to graphics device
ggmap(mapImageData3,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude") +
  geom_polygon(aes(x=long,
                   y=lat,
                   group=group),
               data=IsraelPoints,
               color=colors[9],
               fil=  colors[6],
               alpha=0.5) +
  labs(x="Longitude",
       y="Latitude")
#############################      
ILshape2 <- rgdal::readOGR(dsn="./includes/ISR_adm", layer="ISR_adm1")
rgdal::readOGR(dsn="./includes/ISR_adm", layer="ISR_adm1",
disambiguateFIDs = TRUE)




</end>
>> output:graphic:  map w/ lightly colored polygons overlayed.
>> output:text:
OGR data source with driver: ESRI Shapefile
Source: "./includes/ISR_adm", layer: "ISR_adm1"
with 7 features
It has 9 fields
An object of class "SpatialPolygonsDataFrame"
Slot "data":
  ID_0 ISO NAME_0 ID_1    NAME_1 TYPE_1 ENGTYPE_1 NL_NAME_1
0  111 ISR Israel    1     Golan  Mehoz  District      <NA>
1  111 ISR Israel    2   HaDarom  Mehoz  District      <NA>
2  111 ISR Israel    3     Haifa  Mehoz  District      <NA>
3  111 ISR Israel    4  HaMerkaz  Mehoz  District      <NA>
4  111 ISR Israel    5   HaZafon  Mehoz  District      <NA>
5  111 ISR Israel    6 Jerusalem  Mehoz  District      <NA>
6  111 ISR Israel    7  Tel Aviv  Mehoz  District      <NA>
             VARNAME_1
0                 <NA>
1    Southern District
2                 Hefa
3     Central District
4             Northern
5 Al-Quds|Yerushalayim
6                 <NA>

_______________________________________________
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: noob question: access single region from .shp file

Kent Johnson
On Thu, Mar 9, 2017 at 6:00 AM, <[hidden email]> wrote:

>
> Message: 2
> Date: Wed, 08 Mar 2017 15:45:45 +0200
> From: Brandon Payne <[hidden email]>
> To: [hidden email]
> Subject: [R-sig-Geo] noob question: access single region from .shp
>         file
> Message-ID: <[hidden email]>
> Content-Type: text/plain
>
> How do you access a single region in a shape file?
>

See changes below.

Kent

## libraries

> library(maptools)
> library(RColorBrewer)
> colors <- brewer.pal(9, "BuGn")
> library(ggmap)
>
> ## google maps
> mapImageData3 <- get_map(location = c(lon = 35.1660235,
>                                       lat = 31.32226),
>                          color="color",
>                          source = "google",
>                          maptype = "satellite",
>                          zoom = 7)
>
> ## read shape file
> IsraelPolygon <- readShapePoly("./includes/ISR_adm/ISR_adm1.shp")
> IsraelPoints  <- fortify(IsraelPolygon)
>

IsraelPoints <- fortify(IsraelPolygon, , region='NAME_1')

## plot shapes on top of map, output to graphics device

> ggmap(mapImageData3,
>     extent = "device",
>     ylab = "Latitude",
>     xlab = "Longitude") +
>   geom_polygon(aes(x=long,
>                    y=lat,
>                    group=group),
>                data=IsraelPoints,
>                color=colors[9],
>                fil=  colors[6],
>

fill=  ifelse(IsraelPoints$id=='Golan', 'red', colors[6]),


>                alpha=0.5) +
>   labs(x="Longitude",
>        y="Latitude"

        [[alternative HTML version deleted]]

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