[R] [External] Re: Removing polygons from shapefile of Scotland and Islands

Barry Rowlingson b@row||ng@on @end|ng |rom gm@||@com
Fri May 17 13:49:06 CEST 2024


Scotland is the second feature in the UK data, so get it and split this one
MULTIPOLYGON feature into individual POLYGONS

scot = st_cast(the_uk$geometry[2],"POLYGON")

# which is the largest polygon?
which.max(st_area(scot))
[1] 1

# the first one. ok...

plot(scot[[1]]) # mainland

# add the rest of the islands for context, in grey, maybe to show they're
outside our study area:

for(i in 2:length(scot)){plot(scot[[i]], col="grey", add=TRUE)}

There are 2794 polygons in Scotland (according to this data)...

Barry




On Tue, May 14, 2024 at 4:40 PM Jan van der Laan <rhelp using eoos.dds.nl> wrote:

> This email originated outside the University. Check before clicking links
> or attachments.
>
> I believe mapshaper has functionality for removing small 'islands'.
> There is a webinterface for mapshaper, but I see there is also an
> R-package (see
>
> https://search.r-project.org/CRAN/refmans/rmapshaper/html/ms_filter_islands.html
> for island removal).
>
> If you want to manually select which islands to keep and which to
> remove, you can split multipolygons into single polygons. I believe that
> is possible using st_cast.
>
> But if it is just getting the relevant portion of the map on screen.
> With the plot-command and using st_viewport it is possible to set the
> part of the map that is drawn.
>
> HTH,
> Jsn
>
>
> On 14-05-2024 15:16, Nick Wray wrote:
> > Hello  I have a shapefile of Scotland, including the islands.  The river
> > flow data I am using is only for the mainland and for a clearer and
> larger
> > map I would like to not plot Orkney and Shetland to the north of the
> > mainland, as I don't need them.
> >
> > The map I have I got from
> >
> https://borders.ukdataservice.ac.uk/easy_download_data.html?data=infuse_ctry_2011
> >
> > then I put the uk shapefile onto my laptop with no problems (I have sf
> > running)
> >
> >
> the_uk<-st_read(dsn="C:/Users/nickm/Desktop/Shapefiles/infuse_ctry_2011.shp")
> >
> > scotland<-the_uk[2,]
> >
> > plot(scotland$geometry)
> >
> > This gives me a nice map of Scotland  plus islands but obviously there
> are
> > lots of separate polygons and if I go into the points with something like
> >
> > scot_pts<-unlist(as.data.frame(scotland$geometry))
> >
> > it's not at all clear how I can get rid of the points I don't want as
> they
> > don't seem to be listed in any easy way to find where one polygon stops
> and
> > another starts
> >
> > I am wondering whether this approach is right anyway or whether there is
> > some sf function which would allow me to identify the polygons I want -
> > essentially the big one which is the mainland without lots of elaborate
> > conversions and manipulations
> >
> > Any pointers, thoughts etc much appreciated
> >
> > Thanks Nick Wray
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list