0

I have a 156Mb shapefile of New Zealand polygons down to the individual suburb level. There are 7,369 polygons. Each polygon has a unique ID (1...7369) in an ID column. There is a Suburb column associated with each ID, however the suburb names are not unique. For example, there is an "Avondale" Suburb in "Auckland" as well as an "Avondale" Suburb in "Christchurch". But their ID differs, along with their Town.

The Town column contains the name of the town--if there is one. Because each Town has more than one Suburb, most Town names are duplicated over multiple ID rows. Continuing the example above, the two "Avondale"s have unique IDs and are assigned to a different Town ("Auckland" in the first case, "Christchurch" in the second).

The Town column has duplicated entries as all relevant Suburbs are allocated to their respective Town. For example, the Town "Auckland" has around 150 Suburb rows (each with a unique ID).

I also have a data file of events that have been recorded, sometimes down to the Suburb level, but often only to the Town level. Because of this, and to protect the confidentiality of people who have provided event information, I would like to map the events to the Town level. I am trying to produce a choropleth map using the package leaflet in R.

Unfortunately, it is not a simple matter of aggregating the Town column. Some of the Suburbs are features such as lakes, forests, bodies of water in the sea, mountains, and have no Town associated with them: the Town column is NA. They are also discontinuous polygons. Each Town, however, will form a continuous polygon if merged by Town name. The Town names are unique. For example, all "Wellington" (Town) suburbs map to "Wellington", and there is no second Town called Wellington in the shapefile.

I have done a variable, EventCount, that contains the summed record counts for each Town. I have assigned 0 to each Town with no events. But I don't want to map all the individual Suburb polygons - I end up with a 100Mb html file--I want to publish this to the web, I'm using saveWidget to export the html files-- with the same event count repeated across all polygons that make up the Town. I also end up with most polygons associated with no events, and therefore irrelevant to the map.

How can I merge just the Suburbs that are linked to Towns, and keep the rest of the data at an individual polygon level? To cut down on the polygons, and the size of files, I am also happy to restrict my NZ shapefile to only polygons that have towns associated with them. The latter option will decrease my polygon count from 7369 polygons to 607, and shapefile size from 156Mb down to 50Mb.

I'm using stamen.TonerLite for the addProviderTiles. This gives me a good shape of New Zealand to overlay my polygons.

The shapefile data is proprietary and so I cannot share it. A mock example is (without being a shapefile):

ID <- c(1,2,3,4,5,6,7,8,9,10)
Suburb <- c("Addington", "Akaroa", "Alicetown", "Anapai Bay", "Avondale", "Avondale", "Benford Bay", "Big Island", "Bryan Bay", "Chasm Island")
BiggerThanSuburb <- c("NA", "NA", "NA", "NA", "NA", "NA", "NA", "Boat", "NA", "NA")
EvenBiggerThanSuburb <- c("NA", "NA", "NA", "NA", "NA", "NA", "NA", "Muttonbird", "Manakau", "NA") 
EvenBiggerStill <- c("NA", "NA", "NA", "NA", "NA", "NA", "Matahi", "NA", "NA", "NA")
Town <- c("Christchurch", "Akaroa", "Lower Hutt", "NA", "Auckland", "Christchurch", "NA", "NA", "NA", "NA")
Foo <- data.frame(ID, Suburb, BiggerThanSuburb, EvenBiggerThanSuburb, EvenBiggerStill, Town)

How do I keep the Town data, including 0 event counts, but merge the Suburb polygons into the appropriate Town - but only those where the ID has a Town?

How do I handle the polygons with no Town? As you can see, the only variables that contain content across all records are ID and Suburb.

To make things a bit more complicated, R decided all character variables were factors when the shapefile import was done. I've been using as.character to handle this.

I'm fine with merging the count data in, it's getting the shapefile ready that has me stumped.

Edit for update: I have tried to use rgeos with the following code:

FooWithTownsOnly <- subset(Foo, Town!="") #get rid of the many shapes with no Town
FooTownOnlyPolygons <- gUnaryUnion(FooWithTownsOnly, id=FooWithTownsOnly@data$Town)

This gives me a Large SpatialPolygons which includes only the 607 Town elements. How do I get a Large SpatialPolygonsDataFrame to use?

I've been following the instructions here. I think I have done the dissolve correctly, I can't work out what to do next. I can't even see if the polygons have been merged correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.