SlideShare una empresa de Scribd logo
1 de 61
Geospatial Data in R

 Geospatial Data
 in R
 And Beyond!
 Barry Rowlingson
 b.rowlingson@lancaster.ac.uk




 School of Health and Medicine,
 Lancaster University
Spatial Data in R




Historical
perspective
Before R
The S Language (Chambers, Becker, Wilks)
  Born 1976
  Released 1984
  Reborn 1988 (The New S Language)
S-Plus
  Commercialised version, 1988
                                     Image from amazon.com
points, polygon, lines
“Roll your own” maps
plot(xy,asp=1)
polygon(xy,col=...)
points(xy,pch=...)
lines(xy,lwd=2,...)
First Maps
The map package: Becker and Wilks
library(maps);map()
map(database,regions,...)
map database format
                 To use your data
                  with the maps
    map.line
                  package, first
                  create these three
    map.gon       files...


   map.names
map database format
                    2     1
               4.07789 5.58567 4.04461 5.52577 3.91483
               5.5291 3.86491 5.47585 3.79169 5.42926
               3.78171 5.33941 3.80168 5.2828
    map.line   3 3.86491 5.17634 3.88487 5.09314 3.99137
               5.07317 4.13447 5.14306 4.17773 5.16303
               4.28755 5.13973 4.35078 5.0
               765 4.36077 5.12642
    map.gon    EOR
                    3     1
               4.36077 5.12642 4.37075 5.12642 4.47392
               5.11643 4.5438 5.14306 4.71686 5.11643
               4.77343 5.15304 4.77676 5.1996
   map.names   3 4.69356 5.23624 4.61369 5.23624 4.57043
               5.26619 4.51052 5.22626 4.51385 5.28616
               4.49388 5.3627 4.46061 5.38
               932 4.46726 5.38932
               EOR
map data format


    map.line
               1 2 3 4
               EOR
               1 5 3 -6
    map.gon    EOR
               2 -5
               EOR


   map.names
map data format


    map.line




    map.gon
               Scotland 1
               Scotland 2
               Scotland:Harris   3
               Scotland:Harris   4
   map.names   Scotland:Harris   5
               Scotland:Harris   6
map data format
                   Then process
                    it with
    map.line        mapgetl                            map.L

                    and
    map.gon
                    mapgetg                           map.G




   map.names                                           map.N




               maps package now maintained by Ray Brownrigg
New Millennium
GIS spreading
Many more map file formats
Maps on the web
Spatial Data Standards:
Vector data - the sp classes
2004 : A Geospatial Odyssey
More than just polygons!
  Classes for points, lines, grids of points and
   polygons.
Geometry + Data for each feature
Bivand, Pebesma, Gomez-Rubio
“Simple Features” OGC spec
Create Points
coords = cbind(x, y)

# create SpatialPoints
sp = SpatialPoints(coords)

# create a SpatialPointsDataFrame
spdf = SpatialPointsDataFrame(coords, data)
spdf = SpatialPointsDataFrame(sp, data)

# promote an ordinary data frame
coordinates(data) = cbind(x, y)
coordinates(data) = ~lon + lat

# and demote back to data frame
data = as.data.frame(data)
Point Features
  SpatialPointsDataFrame

                           ID   coordinate   Name            Pop

                           1    (x,y)        Alphaville      12000

                           2    (x,y)        Betaton         45255

                           3    (x,y)        Gamma CIty      5345

                           4    (x,y)        Delta VIllage   7745

                           5    (x,y)        Epsilondon      77514
Manipulation
                                                 Treat like a
                                                 data frame...

  BigTowns = Towns[Towns$pop>10000,]
  SmallTowns = Towns[Towns$pop<5000,]

  BigTowns = subset(Towns,pop>10000)          ...almost (this fails)

  BigSmall = rbind(BigTowns,SmallTowns)

                                       Retrieve coords and data
  xy = coordinates(Towns)

  TownData = Towns@data

  TownData = as.data.frame(Towns)
Create Line Objects
 A Line is a single chain of points


                                      L1 = Line(cbind(x1,y1))




                                        L2 = Line(cbind(x2,y2))



                                        L3 = Line(cbind(x3,y3))
Create Lines Objects
 A Lines is a list of chains with an ID


                                          Ls1 = Lines(list(L1),ID=”a”)




                                            Ls2 = Lines(list(L2,L3),id=”b”)
Create SpatialLines Object
 A SpatialLines is a list of Lines




                                     SL12 = SpatialLines(list(Ls1,Ls2))
Create SpatialLinesDataFrame
 A SpatialLinesDataFrame is a SpatialLines with a matching Data Frame




                                    SLDF = SpatialLinesDataFrame(
                                              SL12,
                                              data.frame(
                                                 Z=c(“road”,”river”),
                                                 row.names=c(“a”,”b”)
                                              )
                                       )
Line Features
           SpatialLinesDataFrame

                 ID   coordinate           Name            Quality

                 1    (x,y;x,y;x,y;...)    River Alpha     Good

                 2    (x,y;x,y;x,y;...)    Bravo Beck      Good

                 3    (x,y;x,y;x,y;...)    Charlie River   Fair

                 4    (x,y;x,y;...)        Delta Drains    Poor
                      (x,y;x,y;...)(...)
                 5    (x,y;x,y;x,y;)       Echo Stream     Good
Polygons

      Now it starts to get complicated
Single Ring Feature

Make sure your coordinates connect:

c1 = cbind(x1,y1)
r1 = rbind(c1,c1[1,])

A Polygon is a single ring

P1 = Polygon(r1)


A Polygons is a list of Polygon objects with an ID

Ps1 = Polygons(list(P1,ID=”a”))
Double Ring Feature
Make sure your coordinates connect:

c2a   =   cbind(x2a,y2a)
r2a   =   rbind(c2a,c2a[1,])
c2b   =   cbind(x2b,y2b)
r2b   =   rbind(c2b,c2b[1,])


Make two single rings:

P2a = Polygon(r2a)
P2b = Polygon(r2b)


Make a Polygons object with two rings

Ps2 = Polygons(list(P2a,P2b),ID=”b”))
Feature with hole
hc = cbind(xh,yh)
rh = rbind(hc,hc[1,])


Make a holey Polygon:

H1 = Polygon(rh, hole=TRUE)


Make a Polygons object with two rings, one the hole

Ps3 = Polygons(list(P3,H1),ID=”c”))
SpatialPolygonsDataFrame

SPs = SpatialPolygons(
   list(Ps1,Ps2,Ps3)
)




SPDF = SpatialPolygonsDataFrame(
   SPs,
   data.frame(
     info=c(”single”,”double”,”hole”),
     row.names=c(”a”,”b”,”c”)
     )
   )
And back again

 For ring j of feature i:

 > SPDF@polygons[[i]]@Polygons[[j]]@coords
            x      y
  [1,] 42.53623 76.85066
  [2,] 42.57308 76.79820
  [3,] 43.05132 74.56176
 ...
Polygon Features
                      SpatialPolygonsDataFrame

              ID   coordinates          Name             Pop.

              1    (x,y;x,y;x,y;...)    Alphashire       12365

              2    (x,y;x,y;x,y;...)    Bravoshire       45366

              3    (x,y;x,y;x,y;...)    County Charlie 78725

              4    (x,y;x,y;...)        Delta District   77439
                   (x,y;x,y;...)(...)
              5    (x,y;x,y;x,y;)       Echoville        34545
Coordinate Systems
Because the Earth
 isn't flat
But small parts of it
 are near enough
So lots of coordinate
 systems exist
Anything Spatial*
 can have one
CRS and proj4string
without:
sp = SpatialPoints(coords)

at construction time:
sp = SpatialPoints(
             coords,
             proj4string = CRS(”+init=epsg:4326”)
        )

After the event:
proj4string(sp) = CRS(”+init=epsg:4326”)
And for Spatial*DataFrames

spdf = SpatialPointsDataFrame(
   sp,
   data,
   proj4string=CRS(”+init=epsg:4326”)
   )

proj4string(spdf)=CRS(”+init=epsg:4326”)
What are these proj4strings?
International standard codes for coordinate
  reference systems
+init=epsg:4326 lat-long, GPS
+init=epsg:27700 UK Grid
+init=epsg:900913 “google”
+init=epsg:3857 Spherical Mercator
+proj=utm +zone=19 +north
 Search spatialreference.org for UTM zones
Transform Spatial* Objects
Assigning a CRS doesn't change the numbers
So assigning the wrong CRS really messes things
 up
Convert from one to another with spTransform
If sp is SpatialPoints from my GPS:
proj4string(sp)=CRS(”+init=epsg:4326”)
And to put on a UK National Grid map:
spUK = spTransform(sp,CRS(”+init=epsg:27700”))
Raster Data – regular grids
Satellite Imagery
  Photography
  Radar Height
  Other Sensors
Count Data
  Animals in quadrats
Modelled Values
  Climate Data
  Interpolations
Rasters in R - history
The image function
Works on a matrix
X and Y vectors
 define position
Volcano?
Volcano
The raster package
Create from vector x, vector y, matrix z
r = raster(list(x=x,y=y,z=z))
plot(r)
The raster package
Read and plot standard raster files
dem = raster('cumbriaDem.tif')
plot(dem)
Raster ops
highGround = dem > 600
slope = terrain(dem,opt='slope')
aspect = terrain(dem,opt='aspect')
hill = hillShade(slope,aspect,40,270)
plot(hill,col=grey(0:100/100)
      ,legend=FALSE)
plot(dem,
  col=rainbow(25,alpha=0.35),
  add=TRUE)
Categorical rasters
use = raster('cumbria.tif');plot(use)
> as.matrix(table(values(use)))
    [,1]
2     325
7       4
11    121
18 24399
21    425
23 5087
24 3084
[etc]
3d visuals – rasterVis
Drape land-use over
 dem
  plot3D(dem,
     drape=use)
Projections and CRS
> use = raster('/data/CorineLandCover/cumbria.tif')
> plot(settlements)
> plot(use)
Projection

 Image grid is up-down
 left right
Projections
rasters have a CRS

> use = raster('/data/CorineLandCover/cumbria.tif')
> use
class       : RasterLayer
dimensions : 278, 285, 79230 (nrow, ncol, ncell)
resolution : 100, 100 (x, y)
extent      : 3466700, 3495200, 3538200, 3566000
coord. ref. : +proj=laea +lat_0=52 +lon_0=10
              +x_0=4321000 +y_0=3210000 +ellps=GRS80
              +units=m +no_defs
values      : /data/CorineLandCover/cumbria.tif
min value   : 2
max value   : 41
> projection(use)
 [1] "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000
+y_0=3210000 +ellps=GRS80 +units=m +no_defs"
Reproject Raster
Involves a 'warp'
> useOS = projectRaster(use,res=100,
     crs=”+init=epsg:27700”,method="ngb")
Better to transform points
 > settlementsP = spTransform(settlements,
                       CRS(projection(use)))
Points and Raster
Now lets go to work
Data created
Coordinates corrected


Lets do some analysis!
Settlements and Land Cover
Point-raster overlay

> use = raster('cumbria.tif')
> plot(use)
> points(settlements,pch=19,cex=0.5)
> extract(use,settlements)
1900 1912 1931 1937
                               Point ID
  18   18   18   18
1958 1964 1976 1980
  23   18   18   23
[etc]
                               Land use code
Buffering with rgeos
>   settbuff=gBuffer(settlements,width=1000)
>   plot(use)
>   plot(settbuff,add=TRUE,lty=2)
>   table(extract(use,settbuff))

   2     11   18          21   23    24   ...
 243      5 9730         173 2346   356   ...

                    category



        Number of grid cells
Per-feature buffers

>   settbuff=gBuffer(settlements,width=1000,byid=TRUE)
>   plot(use)
>   plot(settlements,add=TRUE)
>   plot(settbuff,add=TRUE,lty=2)

> e = extract(use,settbuff)
> et = lapply(e,table)
Extracting The Values
 e = extract(use,settbuff)
                        [[1]]
                          [1]   18 18 18 18 18
  Pixels under town 1     [6]   18 18 18 18 18
         buffer          [11]   26 26 26 18 18
                         [16]   18
                        […]
                        [[2]]
  Pixels under town 2     [1]   25   25   18   18   18
         buffer           [6]   25   25   25   18   18
                         [11]   18   18   18   18   18
                         [16]   18   18   25   25   25
                         [21]   18   18   18   18   18
                        ...
Tabulating the values
   et = lapply(e,table)
                                   ...
                                   [[55]]

                                     18 23     26 27      41
 Category count for town 55
                                     60 112     1 109     32

                                   [[56]]

                                    18    23   26    27   41
                                   180    51    7    36   33

                                   [[57]]

                                    18 26 27
                                    71       3 124
 With a bit more massage and manipulation...
                                  ...
New SpatialPoints
> suse
           coordinates Sport. Pastur Discon Broad. Conife      NAME
1   (3471640, 3538260)      0    141      0      0      0      Gawthwaite
2   (3468280, 3539190)      0    302      0      0      0      Grizebeck
3   (3475820, 3538700)      0    224      0     28      0      Colton
4   (3473380, 3539270)      0    293      0      0      0      Lowick
5   (3481840, 3538800)      0     73      0    158      1      Lakeside
6   (3476400, 3539870)      0    311      0      0      0      Oxen Park
7   (3490200, 3538250)      0    131      0     34      0      The Howe

> spplot(suse,c('Pastur','Broad.'))
                                                            Grid cell counts
Question
Find all the towns that
  have any conifer
  forest within 1km
Make a map showing
 how much conifer
 forest is around
 each town
Map presentation
> hasConifer = suse$Conif>0
> plot(use)
> points(suse[hasConifer,],
    cex=suse[hasConifer,]$Conif/20,
    pch=21,col="black",bg="#ffffff80")
> text(coordinates(suse[hasConifer,]),
    as.character(suse[hasConifer,]$NAME),pos=1)
Automation
Write it all up into a function
 nearBy      = function(
            towns,
            landUse,
            code,
            dist=1000,
            thresh=0){
 …
 }

 mapNearBy(settlements,landUse,
Talk Title
End of Part One
Stay tuned for more!

Más contenido relacionado

La actualidad más candente

Better Visualization of Trips through Agglomerative Clustering
Better Visualization of  Trips through     Agglomerative ClusteringBetter Visualization of  Trips through     Agglomerative Clustering
Better Visualization of Trips through Agglomerative Clustering
Anbarasan S
 
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Computer Science Club
 
shi_summer_2015
shi_summer_2015shi_summer_2015
shi_summer_2015
Tiger Shi
 

La actualidad más candente (19)

Big datacourse
Big datacourseBig datacourse
Big datacourse
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience Specialisation
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in Heaven
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in Heaven
 
ClusterAnalysis
ClusterAnalysisClusterAnalysis
ClusterAnalysis
 
A Multidimensional Distributed Array Abstraction for PGAS (HPCC'16)
A Multidimensional Distributed Array Abstraction for PGAS (HPCC'16)A Multidimensional Distributed Array Abstraction for PGAS (HPCC'16)
A Multidimensional Distributed Array Abstraction for PGAS (HPCC'16)
 
Better Visualization of Trips through Agglomerative Clustering
Better Visualization of  Trips through     Agglomerative ClusteringBetter Visualization of  Trips through     Agglomerative Clustering
Better Visualization of Trips through Agglomerative Clustering
 
Graph Regularised Hashing
Graph Regularised HashingGraph Regularised Hashing
Graph Regularised Hashing
 
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
Andrew Goldberg. Highway Dimension and Provably Efficient Shortest Path Algor...
 
shi_summer_2015
shi_summer_2015shi_summer_2015
shi_summer_2015
 
DATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGESDATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGES
 
distance_matrix_ch
distance_matrix_chdistance_matrix_ch
distance_matrix_ch
 
DASH: A C++ PGAS Library for Distributed Data Structures and Parallel Algorit...
DASH: A C++ PGAS Library for Distributed Data Structures and Parallel Algorit...DASH: A C++ PGAS Library for Distributed Data Structures and Parallel Algorit...
DASH: A C++ PGAS Library for Distributed Data Structures and Parallel Algorit...
 
Map reduce and the art of Thinking Parallel - Dr. Shailesh Kumar
Map reduce and the art of Thinking Parallel   - Dr. Shailesh KumarMap reduce and the art of Thinking Parallel   - Dr. Shailesh Kumar
Map reduce and the art of Thinking Parallel - Dr. Shailesh Kumar
 
Cartographic Resources Cataloging for Beginners Slides
Cartographic Resources Cataloging for Beginners SlidesCartographic Resources Cataloging for Beginners Slides
Cartographic Resources Cataloging for Beginners Slides
 
Learning to Project and Binarise for Hashing-based Approximate Nearest Neighb...
Learning to Project and Binarise for Hashing-based Approximate Nearest Neighb...Learning to Project and Binarise for Hashing-based Approximate Nearest Neighb...
Learning to Project and Binarise for Hashing-based Approximate Nearest Neighb...
 
Parametric surface visualization in Directx 11 and C++
Parametric surface visualization in Directx 11 and C++Parametric surface visualization in Directx 11 and C++
Parametric surface visualization in Directx 11 and C++
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 

Destacado

R programming language in spatial analysis
R programming language in spatial analysisR programming language in spatial analysis
R programming language in spatial analysis
Abhiram Kanigolla
 
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
Guy Lansley
 

Destacado (12)

R programming language in spatial analysis
R programming language in spatial analysisR programming language in spatial analysis
R programming language in spatial analysis
 
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy LansleyUsing R to Visualize Spatial Data: R as GIS - Guy Lansley
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
 
Maps with leafletR
Maps with leafletRMaps with leafletR
Maps with leafletR
 
QGIS & Inkscape: Carographic Tools for Attractive Maps
QGIS & Inkscape: Carographic Tools for Attractive MapsQGIS & Inkscape: Carographic Tools for Attractive Maps
QGIS & Inkscape: Carographic Tools for Attractive Maps
 
Is it harder to find a taxi when it is raining?
Is it harder to find a taxi when it is raining? Is it harder to find a taxi when it is raining?
Is it harder to find a taxi when it is raining?
 
TowardsCognitive BPMas a Platform for Smart Process Support over Unstructured...
TowardsCognitive BPMas a Platform for Smart Process Support over Unstructured...TowardsCognitive BPMas a Platform for Smart Process Support over Unstructured...
TowardsCognitive BPMas a Platform for Smart Process Support over Unstructured...
 
Modeling Count-based Raster Data with ArcGIS and R
Modeling Count-based Raster Data with ArcGIS and RModeling Count-based Raster Data with ArcGIS and R
Modeling Count-based Raster Data with ArcGIS and R
 
Using R for Climate Raster Data Extraction
Using R for Climate Raster Data ExtractionUsing R for Climate Raster Data Extraction
Using R for Climate Raster Data Extraction
 
Inkscape cartography
Inkscape cartographyInkscape cartography
Inkscape cartography
 
New analytical methods for geocomputation - Guy Lansley, UCL
New analytical methods for geocomputation - Guy Lansley, UCLNew analytical methods for geocomputation - Guy Lansley, UCL
New analytical methods for geocomputation - Guy Lansley, UCL
 
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
Linking Socio-economic and Demographic Characteristics to Twitter Topics - Gu...
 
An introduction to structural equation models in R using the Lavaan package
An introduction to structural equation models in R using the Lavaan packageAn introduction to structural equation models in R using the Lavaan package
An introduction to structural equation models in R using the Lavaan package
 

Similar a Geospatial Data in R

Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Vyacheslav Arbuzov
 

Similar a Geospatial Data in R (20)

R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
10. R getting spatial
10.  R getting spatial10.  R getting spatial
10. R getting spatial
 
2024.03.22 - Mike Heddes - Introduction to Hyperdimensional Computing.pdf
2024.03.22 - Mike Heddes - Introduction to Hyperdimensional Computing.pdf2024.03.22 - Mike Heddes - Introduction to Hyperdimensional Computing.pdf
2024.03.22 - Mike Heddes - Introduction to Hyperdimensional Computing.pdf
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with R
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
R
RR
R
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
 
R Workshop for Beginners
R Workshop for BeginnersR Workshop for Beginners
R Workshop for Beginners
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting Spatial
 
R getting spatial
R getting spatialR getting spatial
R getting spatial
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Rug hogan-10-03-2012
Rug hogan-10-03-2012Rug hogan-10-03-2012
Rug hogan-10-03-2012
 
Text Mining Applied to SQL Queries: a Case Study for SDSS SkyServer
Text Mining Applied to SQL Queries: a Case Study for SDSS SkyServerText Mining Applied to SQL Queries: a Case Study for SDSS SkyServer
Text Mining Applied to SQL Queries: a Case Study for SDSS SkyServer
 
Python grass
Python grassPython grass
Python grass
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
 
Python for R users
Python for R usersPython for R users
Python for R users
 
Igraph
IgraphIgraph
Igraph
 
Ml lesson 4 1
Ml lesson 4 1Ml lesson 4 1
Ml lesson 4 1
 
R lecture oga
R lecture ogaR lecture oga
R lecture oga
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Geospatial Data in R

  • 1. Geospatial Data in R Geospatial Data in R And Beyond! Barry Rowlingson b.rowlingson@lancaster.ac.uk School of Health and Medicine, Lancaster University
  • 2. Spatial Data in R Historical perspective
  • 3. Before R The S Language (Chambers, Becker, Wilks) Born 1976 Released 1984 Reborn 1988 (The New S Language) S-Plus Commercialised version, 1988 Image from amazon.com
  • 4. points, polygon, lines “Roll your own” maps plot(xy,asp=1) polygon(xy,col=...) points(xy,pch=...) lines(xy,lwd=2,...)
  • 5. First Maps The map package: Becker and Wilks library(maps);map()
  • 7. map database format To use your data with the maps map.line package, first create these three map.gon files... map.names
  • 8. map database format 2 1 4.07789 5.58567 4.04461 5.52577 3.91483 5.5291 3.86491 5.47585 3.79169 5.42926 3.78171 5.33941 3.80168 5.2828 map.line 3 3.86491 5.17634 3.88487 5.09314 3.99137 5.07317 4.13447 5.14306 4.17773 5.16303 4.28755 5.13973 4.35078 5.0 765 4.36077 5.12642 map.gon EOR 3 1 4.36077 5.12642 4.37075 5.12642 4.47392 5.11643 4.5438 5.14306 4.71686 5.11643 4.77343 5.15304 4.77676 5.1996 map.names 3 4.69356 5.23624 4.61369 5.23624 4.57043 5.26619 4.51052 5.22626 4.51385 5.28616 4.49388 5.3627 4.46061 5.38 932 4.46726 5.38932 EOR
  • 9. map data format map.line 1 2 3 4 EOR 1 5 3 -6 map.gon EOR 2 -5 EOR map.names
  • 10. map data format map.line map.gon Scotland 1 Scotland 2 Scotland:Harris 3 Scotland:Harris 4 map.names Scotland:Harris 5 Scotland:Harris 6
  • 11. map data format Then process it with map.line mapgetl map.L and map.gon mapgetg map.G map.names map.N maps package now maintained by Ray Brownrigg
  • 12. New Millennium GIS spreading Many more map file formats Maps on the web Spatial Data Standards:
  • 13. Vector data - the sp classes 2004 : A Geospatial Odyssey More than just polygons! Classes for points, lines, grids of points and polygons. Geometry + Data for each feature Bivand, Pebesma, Gomez-Rubio “Simple Features” OGC spec
  • 14. Create Points coords = cbind(x, y) # create SpatialPoints sp = SpatialPoints(coords) # create a SpatialPointsDataFrame spdf = SpatialPointsDataFrame(coords, data) spdf = SpatialPointsDataFrame(sp, data) # promote an ordinary data frame coordinates(data) = cbind(x, y) coordinates(data) = ~lon + lat # and demote back to data frame data = as.data.frame(data)
  • 15. Point Features SpatialPointsDataFrame ID coordinate Name Pop 1 (x,y) Alphaville 12000 2 (x,y) Betaton 45255 3 (x,y) Gamma CIty 5345 4 (x,y) Delta VIllage 7745 5 (x,y) Epsilondon 77514
  • 16. Manipulation Treat like a data frame... BigTowns = Towns[Towns$pop>10000,] SmallTowns = Towns[Towns$pop<5000,] BigTowns = subset(Towns,pop>10000) ...almost (this fails) BigSmall = rbind(BigTowns,SmallTowns) Retrieve coords and data xy = coordinates(Towns) TownData = Towns@data TownData = as.data.frame(Towns)
  • 17. Create Line Objects A Line is a single chain of points L1 = Line(cbind(x1,y1)) L2 = Line(cbind(x2,y2)) L3 = Line(cbind(x3,y3))
  • 18. Create Lines Objects A Lines is a list of chains with an ID Ls1 = Lines(list(L1),ID=”a”) Ls2 = Lines(list(L2,L3),id=”b”)
  • 19. Create SpatialLines Object A SpatialLines is a list of Lines SL12 = SpatialLines(list(Ls1,Ls2))
  • 20. Create SpatialLinesDataFrame A SpatialLinesDataFrame is a SpatialLines with a matching Data Frame SLDF = SpatialLinesDataFrame( SL12, data.frame( Z=c(“road”,”river”), row.names=c(“a”,”b”) ) )
  • 21. Line Features SpatialLinesDataFrame ID coordinate Name Quality 1 (x,y;x,y;x,y;...) River Alpha Good 2 (x,y;x,y;x,y;...) Bravo Beck Good 3 (x,y;x,y;x,y;...) Charlie River Fair 4 (x,y;x,y;...) Delta Drains Poor (x,y;x,y;...)(...) 5 (x,y;x,y;x,y;) Echo Stream Good
  • 22. Polygons Now it starts to get complicated
  • 23. Single Ring Feature Make sure your coordinates connect: c1 = cbind(x1,y1) r1 = rbind(c1,c1[1,]) A Polygon is a single ring P1 = Polygon(r1) A Polygons is a list of Polygon objects with an ID Ps1 = Polygons(list(P1,ID=”a”))
  • 24. Double Ring Feature Make sure your coordinates connect: c2a = cbind(x2a,y2a) r2a = rbind(c2a,c2a[1,]) c2b = cbind(x2b,y2b) r2b = rbind(c2b,c2b[1,]) Make two single rings: P2a = Polygon(r2a) P2b = Polygon(r2b) Make a Polygons object with two rings Ps2 = Polygons(list(P2a,P2b),ID=”b”))
  • 25. Feature with hole hc = cbind(xh,yh) rh = rbind(hc,hc[1,]) Make a holey Polygon: H1 = Polygon(rh, hole=TRUE) Make a Polygons object with two rings, one the hole Ps3 = Polygons(list(P3,H1),ID=”c”))
  • 26. SpatialPolygonsDataFrame SPs = SpatialPolygons( list(Ps1,Ps2,Ps3) ) SPDF = SpatialPolygonsDataFrame( SPs, data.frame( info=c(”single”,”double”,”hole”), row.names=c(”a”,”b”,”c”) ) )
  • 27. And back again For ring j of feature i: > SPDF@polygons[[i]]@Polygons[[j]]@coords x y [1,] 42.53623 76.85066 [2,] 42.57308 76.79820 [3,] 43.05132 74.56176 ...
  • 28. Polygon Features SpatialPolygonsDataFrame ID coordinates Name Pop. 1 (x,y;x,y;x,y;...) Alphashire 12365 2 (x,y;x,y;x,y;...) Bravoshire 45366 3 (x,y;x,y;x,y;...) County Charlie 78725 4 (x,y;x,y;...) Delta District 77439 (x,y;x,y;...)(...) 5 (x,y;x,y;x,y;) Echoville 34545
  • 29. Coordinate Systems Because the Earth isn't flat But small parts of it are near enough So lots of coordinate systems exist Anything Spatial* can have one
  • 30. CRS and proj4string without: sp = SpatialPoints(coords) at construction time: sp = SpatialPoints( coords, proj4string = CRS(”+init=epsg:4326”) ) After the event: proj4string(sp) = CRS(”+init=epsg:4326”)
  • 31. And for Spatial*DataFrames spdf = SpatialPointsDataFrame( sp, data, proj4string=CRS(”+init=epsg:4326”) ) proj4string(spdf)=CRS(”+init=epsg:4326”)
  • 32. What are these proj4strings? International standard codes for coordinate reference systems +init=epsg:4326 lat-long, GPS +init=epsg:27700 UK Grid +init=epsg:900913 “google” +init=epsg:3857 Spherical Mercator +proj=utm +zone=19 +north Search spatialreference.org for UTM zones
  • 33. Transform Spatial* Objects Assigning a CRS doesn't change the numbers So assigning the wrong CRS really messes things up Convert from one to another with spTransform If sp is SpatialPoints from my GPS: proj4string(sp)=CRS(”+init=epsg:4326”) And to put on a UK National Grid map: spUK = spTransform(sp,CRS(”+init=epsg:27700”))
  • 34. Raster Data – regular grids Satellite Imagery Photography Radar Height Other Sensors Count Data Animals in quadrats Modelled Values Climate Data Interpolations
  • 35. Rasters in R - history The image function Works on a matrix X and Y vectors define position
  • 38. The raster package Create from vector x, vector y, matrix z r = raster(list(x=x,y=y,z=z)) plot(r)
  • 39. The raster package Read and plot standard raster files dem = raster('cumbriaDem.tif') plot(dem)
  • 40. Raster ops highGround = dem > 600 slope = terrain(dem,opt='slope') aspect = terrain(dem,opt='aspect') hill = hillShade(slope,aspect,40,270) plot(hill,col=grey(0:100/100) ,legend=FALSE) plot(dem, col=rainbow(25,alpha=0.35), add=TRUE)
  • 41. Categorical rasters use = raster('cumbria.tif');plot(use) > as.matrix(table(values(use))) [,1] 2 325 7 4 11 121 18 24399 21 425 23 5087 24 3084 [etc]
  • 42. 3d visuals – rasterVis Drape land-use over dem plot3D(dem, drape=use)
  • 43. Projections and CRS > use = raster('/data/CorineLandCover/cumbria.tif') > plot(settlements) > plot(use)
  • 44. Projection Image grid is up-down left right
  • 45. Projections rasters have a CRS > use = raster('/data/CorineLandCover/cumbria.tif') > use class : RasterLayer dimensions : 278, 285, 79230 (nrow, ncol, ncell) resolution : 100, 100 (x, y) extent : 3466700, 3495200, 3538200, 3566000 coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs values : /data/CorineLandCover/cumbria.tif min value : 2 max value : 41 > projection(use) [1] "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"
  • 46. Reproject Raster Involves a 'warp' > useOS = projectRaster(use,res=100, crs=”+init=epsg:27700”,method="ngb")
  • 47. Better to transform points > settlementsP = spTransform(settlements, CRS(projection(use)))
  • 49. Now lets go to work Data created Coordinates corrected Lets do some analysis!
  • 51. Point-raster overlay > use = raster('cumbria.tif') > plot(use) > points(settlements,pch=19,cex=0.5) > extract(use,settlements) 1900 1912 1931 1937 Point ID 18 18 18 18 1958 1964 1976 1980 23 18 18 23 [etc] Land use code
  • 52. Buffering with rgeos > settbuff=gBuffer(settlements,width=1000) > plot(use) > plot(settbuff,add=TRUE,lty=2) > table(extract(use,settbuff)) 2 11 18 21 23 24 ... 243 5 9730 173 2346 356 ... category Number of grid cells
  • 53. Per-feature buffers > settbuff=gBuffer(settlements,width=1000,byid=TRUE) > plot(use) > plot(settlements,add=TRUE) > plot(settbuff,add=TRUE,lty=2) > e = extract(use,settbuff) > et = lapply(e,table)
  • 54. Extracting The Values e = extract(use,settbuff) [[1]] [1] 18 18 18 18 18 Pixels under town 1 [6] 18 18 18 18 18 buffer [11] 26 26 26 18 18 [16] 18 […] [[2]] Pixels under town 2 [1] 25 25 18 18 18 buffer [6] 25 25 25 18 18 [11] 18 18 18 18 18 [16] 18 18 25 25 25 [21] 18 18 18 18 18 ...
  • 55. Tabulating the values et = lapply(e,table) ... [[55]] 18 23 26 27 41 Category count for town 55 60 112 1 109 32 [[56]] 18 23 26 27 41 180 51 7 36 33 [[57]] 18 26 27 71 3 124 With a bit more massage and manipulation... ...
  • 56. New SpatialPoints > suse coordinates Sport. Pastur Discon Broad. Conife NAME 1 (3471640, 3538260) 0 141 0 0 0 Gawthwaite 2 (3468280, 3539190) 0 302 0 0 0 Grizebeck 3 (3475820, 3538700) 0 224 0 28 0 Colton 4 (3473380, 3539270) 0 293 0 0 0 Lowick 5 (3481840, 3538800) 0 73 0 158 1 Lakeside 6 (3476400, 3539870) 0 311 0 0 0 Oxen Park 7 (3490200, 3538250) 0 131 0 34 0 The Howe > spplot(suse,c('Pastur','Broad.')) Grid cell counts
  • 57. Question Find all the towns that have any conifer forest within 1km Make a map showing how much conifer forest is around each town
  • 58. Map presentation > hasConifer = suse$Conif>0 > plot(use) > points(suse[hasConifer,], cex=suse[hasConifer,]$Conif/20, pch=21,col="black",bg="#ffffff80") > text(coordinates(suse[hasConifer,]), as.character(suse[hasConifer,]$NAME),pos=1)
  • 59. Automation Write it all up into a function nearBy = function( towns, landUse, code, dist=1000, thresh=0){ … } mapNearBy(settlements,landUse,
  • 61. End of Part One Stay tuned for more!

Notas del editor

  1. Before we can talk about spatial statistics, we need to talk about data and problems. So what is spatial data?
  2. Before we can talk about spatial statistics, we need to talk about data and problems. So what is spatial data?