Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to use PBS (I have learnt this the hard way, as the <a href="https://stat.ethz.ch/pipermail/r-sig-geo/2012-June/015433.html" rel="nofollow noreferrer">r-sig-geo</a> link posted by @flowla was a question originally posted by me!). This code shows how to do it all in rgeos, with thanks to <a href="https://stat.ethz.ch/pipermail/r-sig-geo/2013-January/017225.html" rel="nofollow noreferrer"><strong>various</strong></a> different <a href="https://stat.ethz.ch/pipermail/r-sig-geo/2012-June/015340.html" rel="nofollow noreferrer"><strong>postings</strong></a> from Roger Bivand. This would be the more canonical way of subsetting, without resorting to coercion to PolySet objects. </p> <p>The reason for the error message is that you can't gIntersection on a collection of SpatialPolygons, you need to do them individually. Find out which ones you want using <code>gIntersects</code>. I then subset each country polygon using <code>gIntersection</code>. I had a problem passing a list of SpatialPolygons objects back to SpatialPolygons, which turns the croppped shapefiles into SpatialPolygons, which was because not all cropped objects were of <code>class</code> <code>SpatialPolygons</code>. Once we excluded these everything works fine.</p> <pre><code># This very quick method keeps whole countries gI &lt;- gIntersects( WorldMap , clip.extent , byid = TRUE ) Europe &lt;- WorldMap[ which(gI) , ] plot(Europe) #If you want to crop the country boundaries, it's slightly more involved: # This crops countries to your bounding box gI &lt;- gIntersects( WorldMap , clip.extent , byid = TRUE ) out &lt;- lapply( which(gI) , function(x){ gIntersection( WorldMap[x,] , clip.extent ) } ) # But let's look at what is returned table( sapply( out , class ) ) SpatialCollections SpatialPolygons 2 63 # We want to keep only objects of class SpatialPolygons keep &lt;- sapply(out, class) out &lt;- out[keep == "SpatialPolygons"] # Coerce list back to SpatialPolygons object Europe &lt;- SpatialPolygons( lapply( 1:length( out ) , function(i) { Pol &lt;- slot(out[[i]], "polygons")[[1]]; slot(Pol, "ID") &lt;- as.character(i) Pol })) plot( Europe ) </code></pre> <p><img src="https://i.stack.imgur.com/vCZ78.jpg" alt="enter image description here"></p> <p>If you can, I recommend you look at <a href="http://www.naturalearthdata.com/downloads/" rel="nofollow noreferrer">naturalearthdata</a>. They have high quality shapefiles that are kept up-to-date and are constantly checked for errors (since they are open source if you find an error do email them). Country boundaries are under the <em>Cultural</em> buttons. You will see they are also a bit more lightweight and you can choose a resolution appropriate for your needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload