[R-sig-Geo] noob question: access single region from .shp file

Brandon Payne payneb at post.bgu.ac.il
Wed Mar 8 14:45:45 CET 2017


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>



More information about the R-sig-Geo mailing list