Saturday, April 15, 2017

Voting Districts Day 11: Mapping Voting District Shapefiles with Matplotlib

Now that we can create the most basic of maps, let's see if we can build on top of that and apply our layer of voting districts.

Because the voting district shapefile is using a not so traditional projection with latitude and longitude coordinates that you wouldn't necessarily expect, we can't just simply use the straight forward method in matplotlib to apply the shapefile layer to the map.  

Instead, we have to convert the unorthodox coordinates to the traditional projection to match our map's base and then apply that conversion.  That conversion must be a set of polygons (or patches in a patch collections) that we can subplot on top of our basemap consisting of the state of NC.  

import matplotlib.pyplot as plt #what I need to plot stuff to my map.
from mpl_toolkits.basemap import Basemap #what I need to create my basemap.
import shapefile #what I need to read the shapefile from the NC SBE.
from pyproj import Proj #module used to change our projection from the nc to the traditional

from matplotlib.patches import Polygon #used to convert our newly reprojected coordinates to a polygon/patch shape that matplotlib can plot.  
from matplotlib.collections import PatchCollection #we'll be adding all of our voting districts to a patch collection and then plotting that collection.  

import numpy as np #allows us to better interact with arrays, which is the structure used with polygons/ patches.

conv_coords = list() #hold our converted coordinates here as we apply each set of converted coordinates that make up our shape.  
patches = [] #list/collection we'll be sticking our patches/polygons/voting districts into.  This'll pass as the points within our point collection.  

#projection type for the voting districts.
nc = Proj("+proj=lcc +lat_1=34.33333333333334 +lat_2=36.16666666666666 +lat_0=33.75 +lon_0=-79 +x_0=609601.2192024384 +y_0=0 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs ", preserve_units=True)

vote = shapefile.Reader('ncsbe\\Precincts.shp') #creates an instance that has the lists of data we want.
shapes = vote.shapes() #lists of coordinates making up the shape for each voting district.

for x in range(0,len(shapes)): #for each voting district...
    conv_coords.append([]) #apply a new list to our main list consisting of all the shapes.
    for y in range(0,len(shapes[x].points)): #for each set of coords in the shape...
        lon, lat = nc(shapes[x].points[y][0], shapes[x].points[y][1], inverse=True) #convert the shape file points into traditional lat/long coords.
        conv_coords[x].append([lon,lat]) #write the converted coordinates to the new sublist. 
    patches.append(Polygon(np.array(conv_coords[x]), True)) #sublist consisting of all the shapes coordinates is complete.  append to patchcollection list.  

#create our basemap.
m = Basemap(projection= 'cyl', lon_0 = -80, lat_0 = 35, llcrnrlon=-84.9,llcrnrlat=33.5,urcrnrlon=-75.,urcrnrlat=36.6, resolution='i')



#create a figure/subplot that'll be our canvas for the voting districts.  plt is the main plot object.  
fig     = plt.figure()
ax      = fig.add_subplot(111)

#add the general stuff we want on our map that comes with basemap.
m.drawcountries(linewidth=0.5)
m.drawcoastlines(linewidth=0.5)
m.drawstates(linewidth=0.5)

#and with that subplot, apply our patch collection (all of our voting district shapes.
ax.add_collection(PatchCollection(patches, facecolor= 'green', edgecolor='k',  linewidths=0.2, zorder=2))

#create a file with our basemap and plot overlay.
plt.savefig('vote_map_test.png',dpi=600, alpha=True)
#and complete.
plt.close()

and with that, we get a map with the voting districts.  Now we have to figure out how to apply colors to them so we can represent the different districts...


2 comments:

terry james said...

I'm very excited to inform everyone that I'm completely cured from my HSV 1&2 recently. I have used Oregano oil, Coconut oil, Acyclovir, Val acyclovir, Famciclovir , and some other products and it's really help during my outbreaks but I totally got cured from my HSV 2 with a strong and active herbal medicine ordered from a powerful herbalist and it completely fought the virus from my nervous system and I was tested negative after three weeks of usage. I'm here to let y'all know that HSV 1& 2 has a complete cure, I got rid of mine with the help of Dr Moses Buba and his herbal exploit, AFTER I engage with other wrong doctors. Contact him via email: buba.herbalmiraclemedicine@gmail.com or Call/WhatsApp him at +2349060529305
thank you..












I'm very excited to inform everyone that I'm completely cured from my HSV 1&2 recently. I have used Oregano oil, Coconut oil, Acyclovir, Val acyclovir, Famciclovir , and some other products and it's really help during my outbreaks but I totally got cured from my HSV 2 with a strong and active herbal medicine ordered from a powerful herbalist and it completely fought the virus from my nervous system and I was tested negative after three weeks of usage. I'm here to let y'all know that HSV 1& 2 has a complete cure, I got rid of mine with the help of Dr Moses Buba and his herbal exploit, AFTER I engage with other wrong doctors. Contact him via email: buba.herbalmiraclemedicine@gmail.com or Call/WhatsApp him at +2349060529305
thank you..







I'm very excited to inform everyone that I'm completely cured from my HSV 1&2 recently. I have used Oregano oil, Coconut oil, Acyclovir, Val acyclovir, Famciclovir , and some other products and it's really help during my outbreaks but I totally got cured from my HSV 2 with a strong and active herbal medicine ordered from a powerful herbalist and it completely fought the virus from my nervous system and I was tested negative after three weeks of usage. I'm here to let y'all know that HSV 1& 2 has a complete cure, I got rid of mine with the help of Dr Moses Buba and his herbal exploit, AFTER I engage with other wrong doctors. Contact him via email: buba.herbalmiraclemedicine@gmail.com or Call/WhatsApp him at +2349060529305
thank you..

Ruth Maris said...

I use to be a very poor woman who has always not find luck when it comes to playing the lottery. I have been playing lottery since i was 21 years and now i am 45 meaning i have been playing lottery for 24 years. The biggest amount i have ever won in my life was 400 bucks. But one day my story turns to history after i find this man name on the Internet that he is the best when it comes to winning lottery. This man is a very strong voodoo doc who gives out the numbers that can never fail. After all my years of laboring and struggling to win the lottery i finally won ( $27,000,000) Dr Alli is the name, Email: allispellhelp1@gmail.com or contact him on his whatsApp number +2348100772528 this is the only way to win the lottery and the best way