CREATING A ZIP-CODE MAP · PDF fileCREATING A ZIP-CODE MAP WITH COUNTY BORDERS AND CITY...

Preview:

Citation preview

CREATING A ZIP-CODE MAP WITH COUNTY BORDERS AND CITY LABELS

Rachel PotterMSUGOctober 20, 2016

OVERVIEW

From this: To this (a pdf):

proc mapimportproc gmapproc gprojectgoptionsods

1. MAKE A DATASET OF ZIP CODE-LEVEL DATA

READY THE DATA FOR MAPPING

/*coverage by zip code*/data zipcovg;

length zcta5ce10 $5.;set rawdata.msug_zipcovg;zcta5ce10 = zip;

run;proc format;value covgfmt

LOW - < 44.999999999 = 'LOW'45.000000000 - 50.999999999 = 'MEDIUM LOW'51.000000000 - 60.999999999 = 'MEDIUM HIGH'61.000000000 - HIGH = 'HIGH';

run;

2. MAKE A ZIP CODE MAP

ZIP CODE TABULATION AREAS (ZCTA)ZCTAs are generalized areal representations of U.S. Postal Service ZIP Codes

The Census Bureau creates ZCTAs by: Examining all addresses within a census block The most frequently occurring ZIP Code

within each block is assigned to the entire block If a block had addresses, but ZIP Code frequencies

were tied, it is assigned to the ZCTA with which it has the longest shared boundary

Areas without addresses are assigned to the surrounding ZCTA or merged into an adjacent ZCTA with which it has the longest shared boundary

Blocks are aggregated to create larger areas

http://www2.census.gov/geo/pdfs/education/brochures/ZCTAs.pdf

PR(1

Slide 6

PR(1 Potter, Rachel (DHHS), 7/22/2016

DOWNLOAD THE ZCTA SHAPEFILESHTTPS://WWW.CENSUS.GOV/CGI-BIN/GEO/SHAPEFILES/INDEX.PHP

EXTRACT THE FILES

The naming convention is: <TIGER/Line>_<file version>_<state FIPS code>_<layer tag>

tl_2010_26_zcta510.dbf

tl_2010_26_zcta510.prj

tl_2010_26_zcta510.shp

tl_2010_26_zcta510.shp.xml

tl_2010_26_zcta510.shx

MAKE A MAP DATASET

/*convert the shapefile to a map dataset*/

proc mapimport

datafile='T:\MCIR Data Studies\Geography\2010_ZCTA\tl_2010_26_zcta510.shp'

out=mi_zshp;

id zcta5ce10;

run;

MAP THE DATASET

proc gmap data=zipcovg map=mi_zshp;

id zcta5ce10;

choro izcovg / nolegend;

format izcovg covgfmt.;

run; quit;

But Michigan looks squished!And lines are missing!

PROJECT THE MAP DATASET

Projecting is the process of converting coordinates from spherical (latitude and longitude) to Cartesian.

proc gproject projects the latitude and longitude coordinates onto a two-dimensional plane

minimizes distortion of area, distance, direction, and shape

produces an output map dataset for use in proc gmap

proc gmap is designed to plot points on a flat (two-dimensional) plane

GPROJECT PROCEDURE

*map is distorted - need to project;

proc gproject data=mi_zshp degrees eastlong

out=mi_zshp_prj;

id zcta5ce10;

run;

Specifies that the units for X/Y coordinates are degrees

Specifies that the longitude (X) values increase to the east.

Specifies the variable(s) in the input map dataset that identifies unit areas

MAP THE PROJECTED DATASET

proc gmap data=zipcovg map=mi_zshp_prj all;

id zcta5ce10;

choro izcovg / nolegend;

format izcovg covgfmt.;

run; quit;

Specifies that generated maps will include all of the map areas from the map dataset, even if the response dataset does not include an observation for the map area.

3. ADD COUNTY BORDERS

GET A COUNTY MAP

No need to import!

County is one of many SAS-supplied maps

*View SAS-supplied maps;

proc datasets lib=maps;

quit;

CREATE A COUNTY MAP LAYER? NO.pattern color=black value=empty; proc gmap data=maps.counties map=maps.counties;

id county;choro county / nolegend;where state eq 26;

run; quit;/*map is backwards and distorted ‐ need to project*/proc gproject data=maps.counties radians westlong

out=mi_prj;where state eq 26;id state;

run;/*map the projected data*/proc gmap data=mi_prj (obs=1) map=mi_prj;

id county;choro county / nolegend;

run; quit;pattern;

Michigan’s FIPS code

The default

CREATE AN ANNOTATE DATA SET? YES.

Each observation in an annotate data set has complete instructions for drawing, or moving to draw, a graphic

What to do Where to do itHow to do it

HOW TO CREATE AN ANNOTATE DATASETdata mi_cnty_anno;

length function color $ 8;retain xsys ysys '2' color 'black' size 1.75 when 'a' fx fy function;set maps.counties (where=(state=26));by county segment; if first.segment then

do;function = 'MOVE'; FX = X; FY = Y;

end;else if function ^= ' ' then

do;if X = . then

do;X=FX; Y=FY; output; function= ' ';

end;else function = 'DRAW';

end;if function ^= ' ' then

do;output;if last.segment then

do;X = FX; Y = FY; output;

end;end;

run;

‘2’ means that the annotate coordinate system will be the same as the map, so they line up.

Set the color and size of the line drawn

Draw annotation after the map

Retain first X and first Y to connect the last point of each segment back to the first

SIMPLY PROJECT THE ANNOTATE DATA SET AND MAP? NO./*simply project*/

proc gproject data=mi_cnty_anno out=anno_county_prj dupok;

id county;

run;

/*and map!*/

proc gmap data=zipcovg map=mi_zshp_prj annotate=anno_county_prj;

id zcta5ce10;

choro izcovg / nolegend;

format izcovg covgfmt.;

run;

quit;

But they don’t line up!

IT TAKES A FEW STEPS TO FIX THAT…

/*1. convert eastlong degrees to westlong radians in ZCTA data*/data mi_zshp_wr;

set mi_zshp;x=atan(1)/45 * -1*x;y=atan(1)/45 * y;

run;

/*2. add a flag to the county annotate dataset*/data anno_county_flag;

set mi_cnty_anno;anno_flag = 1;

run;

/*3. combine the ZCTA and the county annotate datasets*/data zshp_countyanno;

set mi_zshp_wr anno_county_flag;run;

/*4. project the datasets together*/proc gproject data=zshp_countyanno radians westlong

out=zshp_countyanno_prj dupok;id zcta5ce10;

run;

/*5. split the projected data apart*/data zshp_prj countyanno_prj;

set zshp_countyanno_prj;if anno_flag = 1 then output countyanno_prj;

else output zshp_prj;run;

/*6. make the map*/proc gmap data=zipcovg map=zshp_prj annotate=countyanno_prj;

id zcta5ce10;choro izcovg / nolegend;format izcovg covgfmt.;

run;quit;

A ZIP CODE MAP WITH COUNTY BORDERS!

Counties and zip codes line up!

(Can you see the county lines?)

What about cities?

4. ADD CITY LABELS

CREATE AN ANNOTATE DATASET FOR LABELS

data anno_cities; length function color $8;retain xsys ysys '2' color 'orange' position '5' size 0.75

when 'a' x y function style text;set maps.uscity (where=(state=26));length function color $8;/*convert westlong degrees to westlong radians */x=atan(1)/45 * long;y=atan(1)/45 * lat;anno_flag=2;function='label';style='garamond amt/bold'; text=trim(left(city)); if city in ('Detroit','Grand Rapids','Warren','Lansing','Ann

Arbor', 'Flint','Kalamazoo','Marquette','Sault Sainte Marie', ‘Escanaba’,'Menominee','Muskegon','Pontiac','Saginaw','Traverse City',Ludington','Harbor Springs','Houghton','Lapeer','Jackson', 'Berrien Springs','Port Huron','Flint’,'Manistee','Grayling’, 'Monroe‘,’Owosso’);

run;

COMBINE, PROJECT, SEPARATE

/*combine, project, and separate*/data zshp_county_city;

set mi_zshp_wr anno_county_flag anno_cities;run;

proc gproject data=zshp_county_city out=zshp_county_city_prj dupok;id zcta5ce10;

run;

data mi_zshp anno_county anno_cities;set zshp_county_city_prj;if anno_flag = 1 then output anno_county;

else if anno_flag = 2 then output anno_cities;else output mi_zshp;

run;

MAKE THE MAP

proc gmap data=zipcovg map=zshp_prjannotate=anno_county;

id zcta5ce10;choro izcovg / nolegend anno=anno_cities;format izcovg covgfmt.;

run;quit;

5. TWEAKS AND FINAL TOUCHES

GRAPHICS OPTIONS

If you specify GOPTIONS (graphics options), they will override the default style.

GOPTIONS are global You can put them anywhere in your SAS program For them to affect output from a procedure they must execute before the procedure

GOPTIONS are additive The graphics option remains in effect until you specify the option again in another GOPTIONS

statement Use RESET= to reset the values, or end the SAS session

SPECIFY THE GRAPHICS OPTIONS

/* set goptions and produce map */goptions reset = all;*fill patterns for the map areas (sequentially increasing shades of green);pattern1 v=ms c=cxe8edd5;pattern2 v=ms c=cxbde57a;pattern3 v=ms c=cx749938;pattern4 v=ms c=cx264c14;*format titles and footnote;title font='Times New Roman' height=2.5 '43133142 Coverage by Zip Code, 2016';title2 font='Times New Roman' height = 2 'Children aged 19 through 35 Months';footnote font='Times New Roman' height = 1.25 'For MSUG Meeting';*specify parameters for the legend;legend1origin=(15,20)pct /*x and y coordinates of the lower left corner of the legend box*/across=1 /*the number of columns to use for legend entries*/mode=share /*specifies if the legend is drawn in output area and if it can overlay*/label=(position=top justify=c height=1.5 ‘Level')shape=bar (3,4) pct /*bar with proc gmap, width and height of the legend values, units*/cborder=black /*color of the frame around the legend area*/order=descending /*orders the values that appear in the legend*/value=(height=1.5 justify=l); /*modifies the legend value descriptions, h=height, j=justify*/

http://www.devenezia.com/docs/SAS/sas-colors.html

PRINT THE MAP

ods pdf file = 'C:\Users\PotterR1\Desktop\MSUG_map.pdf' notoc;

proc gmap data=zipcovg map=zshp_prj annotate=anno_county;id zcta5ce10;choro izcovg / discrete coutline=same legend=legend1

annotate=anno_cities;;format izcovg covgfmt.;

run;quit;

ods pdf close;

FINAL PRODUCT

THANK YOU! Questions?

Recommended