mergepoly -- Merge adjacent polygons from a shape boundary file
mergepoly id_var lat lon , [ generate(newvar) ]
mergepoly is used to merge adjacent polygons from a shape boundary file. Shape boundary files are usually obtained from outside sources and converted to Stata datasets using -shp2dta- (SSC). The shape database contains one record per feature and the coordinate dataset describes the shape of each feature in terms of points. The points, in order, form polygons.
mergepoly combines adjacent polygons by removing all adjacent line segments. mergepoly creates new polygons that follow the outer border of the initial adjacent polygons.
id_var is a numeric variable that identifies the geographic feature. lat lon are numeric variables that provide the latitude and longitude coordinates of each point. Note that when using -shp2dta- (available from SSC), the id_var is named by default _ID; the latitude variable is named _Y and the longitude is named _X.
generate() specifies the name of the new numeric variable that identifies the new features that are created from the merged polygons.
Download the Census 2000 County Areas for the State of Texas in ArcView Shapefile (.shp) format
. copy "http://www.census.gov/geo/cob/bdy/co/co00shp/co48_d00_shp.zip" > "co48_d00_shp.zip" . unzipfile "co48_d00_shp.zip" Use -shp2dta- to convert to Stata datasets
. shp2dta using "co48_d00.shp", data("tx_counties.dta") coor("tx_coordi > nates.dta")
Draw the initial shapes
. use "tx_coordinates.dta", clear . line _Y _X, lwidth(vvthin) cmissing(n) aspectratio(.95) xscale(off) y > scale(off) ylabel(minmax, nogrid) xlabel(minmax)
Merge adjacent counties
. mergepoly _ID _Y _X, gen(newid)
Draw the new shapes
. line _Y _X, lwidth(vvthin) cmissing(n) aspectratio(.95) xscale(off) y > scale(off) ylabel(minmax, nogrid) xlabel(minmax)
Show which counties have segments on the borders of the new polygons
. tabulate _ID newid
Robert Picard email@example.com
A question on Statalist from Karsten Pfaff was the stimulus for writing