A Python module for parsing U.S. States from an SVG file, and an example of using this with PyGame.
This is intended to support the SoftDes Spring 2016 Interactive Programming project.
The attached files consist of a module, us_map.py, that defines the shape of each U.S. state;
an SVG file Blank_US_Map.svg from which it reads these shapes;
and a short example pygame_draw_state.py of using PyGame to draw a single state.
Use pip to install these packages:
- BeautifulSoup
- svg.path
- matplotlib
Print the polygon that defines the border of Colorado:
import us_map
print us_map.states['CO']
pygame_draw_state.py is a longer example that draws Colorado in a PyGame window.
This code also demonstrates how to determine if the mouse is inside the state.
us_map.states is a dictionary abbr -> polygon_list, where state is a two-letter
ANSI U.S. abbreviation.
us_map.counties_by_name is a dictionary county -> polygon, where county is a string of the form
"Norfolk, MA".
us_map.counties_by_name is a dictionary county_code -> polygon, where county_code is an integer
FIPS county code.
Each polygon is a list of tuples (x, y), where x and y are float coordinates. These coordinates only have meaning within the map: they aren't longitude and lattitude.
Many states, such as Colorado, consist of a single polygon. Several, such as Massachusetts, consist of several.
The Python files are released under the MIT License.
Blank_US_Map.svg (source) and
USA_Counties_with_FIPS_and_names.svg (source)
are from the Wikipedia and are licensed under the GNU Free Documentation License.