How to delete some polygons from 'SpatialPolygonsDataFrame'
Hi Roger, You are right, it is polygon 1:12 making the remote islands. Thanks for a very detailed example. It worked after I tried your method on my shapefile. I actually used ggplot() after fortify() the data in the first place. The remote islands were easily deleted from the dataframe. However, as you illustrated, if I want to plot the neighbour structure, it is such a mess, and interaction with ggplot is yet unknown to me. Thanks and Regards Fred ?
On Fri, Nov 28, 2014 at 9:38 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
On Fri, 28 Nov 2014, Frede Aakmann T?gersen wrote: Hi
If nsw_lga[153, ] is the island that you do not want to plot then you can do
I think that the problem is that only Polygon objects 1:12 in Polygons
object 153 are the islands, while 13:14 are larger and do not have label
point longitude > 159. One possibility then is to take out the Polygons
object (using Scotland in rgdal as an example and recalling Barry
Rowlingson's insight that someone added slivers of islands to their desired
proximate neighbours - so part of 48 Shetland is on Orkney):
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
plot(scot_BNG)
plot(scot_BNG[48,], col="red", add=TRUE)
library(spdep)
nb <- poly2nb(scot_BNG)
plot(nb, coordinates(scot_BNG), add=TRUE)
If we drop Shetland and Fair Isle (using a latitude cutoff):
scot_BNG1 <- scot_BNG
pols48 <- slot(scot_BNG1, "polygons")[[48]]
pol48 <- slot(pols48, "Polygons")
length(pol48)
sapply(pol48, function(x) {slot(x, "labpt")[2]})
res <- pol48[sapply(pol48, function(x) {slot(x, "labpt")[2]}) < 1050000]
slot(pols48, "Polygons") <- res
slot(scot_BNG1, "polygons")[[48]] <- pols48
plot(scot_BNG1, lwd=3, add=TRUE)
bbox(scot_BNG)
bbox(scot_BNG1)
we appear to get what we want (but the plot order and bounding box of the
SpatialPolygons object are not updated to match the exclusion).
Alternatively, and reconstructing the object, use rgeos:
library(rgeos)
bb <- bbox(scot_BNG)
plot(scot_BNG)
plot(scot_BNG[48,], col="red", add=TRUE)
crds <- cbind(x=c(bb[1, 1]-1, bb[1, 1]-1, bb[1, 2]+1, bb[1, 2]+1,
bb[1, 1]-1), y=c(bb[2, 1]-1, 1050000, 1050000, bb[2, 1]-1, bb[2, 1]-1))
c1 <- SpatialPolygons(list(Polygons(list(Polygon(crds)), "1")),
proj4string=CRS(proj4string(scot_BNG)))
plot(c1, add=TRUE, lty=2)
scot_BNG2a <- gIntersection(scot_BNG, c1, byid=c(TRUE, FALSE))
scot_BNG2b <- SpatialPolygonsDataFrame(scot_BNG2a, data=slot(scot_BNG,
"data"))
plot(scot_BNG2b, lwd=3, add=TRUE)
bbox(scot_BNG)
bbox(scot_BNG2b)
Hope this clarifies,
Roger
plot(nsw_lga[ -153, ]) or you can set an xlim in plot() plot(nsw_lga, xlim = c(140, 153)) you may perhaps need play with the settings of xlim to find the right ones. If you want to have an object without the island then do nsw_lga_wo_island <- nsw_lga[ -153, ] Something like nsw_lga[ -153, ] <- NULL doesn't seem to work. Don't know if something similar will work. Yours sincerely / Med venlig hilsen Frede Aakmann T?gersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 frtog at vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender. -----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of
Jianyun Wu
Sent: 28. november 2014 06:33
To: R-sig-Geo at r-project.org
Subject: [R-sig-Geo] How to delete some polygons from
'SpatialPolygonsDataFrame'
Dear list,
I usually use 'readOGR()' from rgdal package in R to input the ESRI
shapefile.
So the inputted data are SpatialPolygonsDataFrame.
I am processing the New South Wales, Australia map for a spatial
analysis.
There is an island far away from the mainland, which I want to delete it
from the map when I plot NSW. Because it makes the whole plot small due
to
its long distant.
Below is the area contains the polygons of this island and surrounding
islands. There are 14 "@Polygons" contributing to the entire area. From
some experiments, I know that it is Polygon 3-14 making the distant
islands.
Is there a simple way that I can delete those small polygons from this
area
polygon and their attributes from the data
Thank you
Fred
str(nsw_lga[153, ])
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 1 obs. of 3 variables:
.. ..$ STATE_CODE: Factor w/ 9 levels "1","2","3","4",..: 1
.. ..$ LGA_CODE11: Factor w/ 565 levels "10050","10110",..: 153
.. ..$ LGA_NAME11: Factor w/ 564 levels "Adelaide (C)",..: 483
..@ polygons :List of 1
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..@ Polygons :List of 14
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 3.83e-07
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:38, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 4.69e-07
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:13, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 7.82e-07
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:31, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 1.02e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:47, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 1.14e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:22, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.6
.. .. .. .. .. .. ..@ area : num 1.38e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:24, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 1.52e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:26, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 2.05e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:32, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 2.61e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:36, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 4.27e-06
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:109, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.5
.. .. .. .. .. .. ..@ area : num 1.42e-05
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:85, 1:2] 159 159 159 159 159 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 159.1 -31.6
.. .. .. .. .. .. ..@ area : num 0.00152
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:2929, 1:2] 159 159 159 159 159
...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 142.2 -30.7
.. .. .. .. .. .. ..@ area : num 8.79
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:5178, 1:2] 144 144 144 144 144
...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 141.5 -31.9
.. .. .. .. .. .. ..@ area : num 0.0162
.. .. .. .. .. .. ..@ hole : logi TRUE
.. .. .. .. .. .. ..@ ringDir: int -1
.. .. .. .. .. .. ..@ coords : num [1:131, 1:2] 141 141 141 141 141 ...
.. .. .. ..@ plotOrder: int [1:14] 13 14 12 11 10 9 8 7 6 5 ...
.. .. .. ..@ labpt : num [1:2] 142.2 -30.7
.. .. .. ..@ ID : chr "152"
.. .. .. ..@ area : num 8.79
..@ plotOrder : int 1
..@ bbox : num [1:2, 1:2] 141 -33.4 159.1 -29
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +ellps=GRS80 +no_defs"
[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
-- Roger Bivand Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 91 00 e-mail: Roger.Bivand at nhh.no