There’s a ton of material out there on the web by people who are plotting things like push-pins to Virtual Earth maps. But what has always interested me, and has always been somewhat of a struggle, is doing the same for complex polygons like U.S. States and counties. In fact, to date I haven’t run into a single example out there of anyone working with large numbers of polygons, or complex ones at that. And I think for good reason.
And unfortunately, geographic entities like U.S. States fall into the latter category due to all the wonderful natural features that delineate them. We’re not talking about squares and rectangles here. In Virtual Earth, there are ways to reduce accuracy to save on computation time, but users in the era of CNN election poll maps are familiar with these shapes and come to expect them to "look right".
So if that didn’t scare you away and you’re interested in displaying geographic entities like U.S. States, counties, and zip codes using Virtual Earth, you can download the Shapefiles from two places free of charge. Each location is courtesy of the U.S. Census Bureau:
- The Tiger/Line(R) Shapefiles. These Shapefiles are the most detailed out there.
- The Generalized Cartographic Boundary Files. These are simplified Shapefiles and do not include the extended shoreline that the Tiger/Line Shapefiles have.
Assuming you’re working with SQL 2008, you can easily import these files to a SQL table using Morten Nielsen’s excellent Shape2SQL utility. Once you get that done, let’s see what the resulting data looks like in SQL.
If you opted for the Generalized Cartographic Boundary files, you’ll discover the following:
Application Modernization Industry Quick Guides
Application modernization enables you to optimize business processes and transform the way you do business today, and in the future. Our industry experts have collected the latest application modernization trends impacting the automotive, financial services, healthcare, and manufacturing verticals.
That’s 273 rows for only 50 U.S. States! I can tell you know that you typically don’t want to try to draw more than about 100 polygons on a given map. States in Tiger/Line look like this:
That’s one line per state. Granted, several states are of MULTIPOLYGON type, meaning multiple polygons exist for that one entity. But it turns out there are significantly fewer polygons per state in the Tiger/Line data that with the Generalized files. This is due to the fact that the shoreline is extended out three miles from the mainland in the Tiger/Line data. This eliminates all those pesky little islands that end up getting drawn with the Generalized Boundary files. As a rule, the fewer polygons you can get away with drawing, the better.
The drawback to using Tiger/Line is the fact that the shapes are so complex. For example, the Shapefile for Wyoming alone consists of 4,187 points! So you have a choice between a set of simplified shapes that force you into a large number of polygons, or a set of complex shapes that result in fewer polygons (a little over 50). What do you do?
This is an example where the new spatial functionality in SQL 2008 really shines. Since we’re using the geometry datatype, we can apply the following function to the query:
On line 1, the Reduce() function applies the same algorithm used to produce the Generalized Boundary files. In addition, the ToString() function converts the spatial data to Well Known Text (WKT) format, which is need to allow for the data to be used by the Virtual Earth ASP.NET application. Using this technique, you get the best of both worlds.
However, there is one thing to point out. Applying the same tolerance for all U.S. State shapes will not yield good results. More complex shapes then to maintain their expected shapes than simpler ones. Here is a screenshot of the entire map set to a tolerance of .008:
The good news is that took about 5 seconds to run on my laptop. The bad news is the result we get for North Dakota, Montana, Wyoming, Kansas, etc…. Reducing the tolerance to .004 results in slight improvements, but increases the load time to about 20 seconds. That’s longer than I think anyone would want to tolerate. A good way to deal with this is to handle the tolerance for problem shapes on a case by case basis:
On my laptop, this approach has allowed me to draw all 48 continental states in just under 10 seconds. I’ll take it! The polygons aren’t perfect, but the tolerances can always be adjusted, if desired.