Tuesday, 12 November 2019

shapefile - Merge Zip Codes to Create new sales Markets


Let me preface this question by saying I am very novice with respect to GIS. With this in mind, I am looking to merge zip codes into sales territories. Instead of plotting data by zip code, I want to have a new file that lets me plot data for the respective territories.


Currently, I get around this by plotting the same value for all zip codes in the territory.


What is the best/easiest way to do accomplish this task? I am quite familiar with the programming language R, but usually can work through examples if this requires programming in other languages.


Many thanks in advance



Answer



The unionSpatialPolygons function in the maptools package does this. You supply to it a SpatialPolygons and a vector which indicates which polygons (zip codes, in your example) belong to which aggregated polygon (sales areas).


EDIT:


The help page on unionSpatialPolygons has a good example of creating a grouping/indexing vector. It using counties in North Carolina, but the ideas are the same. Pulling from that example:


nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],

proj4string=CRS("+proj=longlat +datum=NAD27"))

Loads an example SpatialPolygonsDataFrame. Extract from this whatever information you need that is one-per-polygon. The example gets single coordinates associated with the polygon (centroid?)


lps <- coordinates(nc1)

You can see these together:


plot(nc1)
points(lps)

enter image description here



You could do something with nc1@data instead. Either have 100 entries, one for each polygon.


> dim(nc1@data)
[1] 100 14
> dim(lps)
[1] 100 2
> length(nc1@polygons)
[1] 100

Then use whatever transformation/mapping to derive a length 100 vector that indicates the groups.





The gpclibPermitStatus error probably means you don't have the rgeos package installed and/or have not agreed to the more restrictive license of gpclib.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...