Now that we've got the center points for our voting district shapes, we need to figure out how to create central points where we can start our clustering.
There is a random library included with Python that I hope can do the job. It has a uniform function that will give me a random float between two numbers that I designate. I'll just loop through to generate these random coordinates based on how many clusters I want to build.
First, I want to make sure these starting coordinates fall somewhere approximately within the state of NC. So, I'm going to get the total min and max x and y axis values by writing ALL of the x and y points to two lists and pull a min and max from those lists to create a range for the random numbers.
import random
...
xmids = list()
ymids = list()
for x in range(0,len(shapes)):
for y in range(0,len(shapes[x].points)):
xmids.append(shapes[x].points[y][0])
ymids.append(shapes[x].points[y][1])
start_points = list()
xmax_o = max(xmids)
xmin_o = min(xmids)
ymax_o = max(ymids)
ymin_o = min(ymids)
From there, we'll use the random.uniform method through a loop to create whatever number of points we want to start with.
start_point_count = 12
for z in range(0,start_point_count):
start_points.append([random.uniform(xmin_o,xmax_o),random.uniform(ymin_o, ymax_o)])
And we now have our random starting points!
Next up, we are going to build ourselves a matrix consisting of the distances between the centroids of our voting district shapes and our starting points. From there, we will then figure out which voting district should be associated with each point by going through the voting districts draft style where each point will take a turn picking its closest voting district.
Showing posts with label pyshp. Show all posts
Showing posts with label pyshp. Show all posts
Wednesday, February 1, 2017
Sunday, January 22, 2017
Voting Districts Day 3: Yet Another Package Change to Pyshp
Still having trouble with Fiona. So, I'm trying another package for reading shapefiles: pyshp.
Pyshp installs through pip without issue (as long as I do it as an admin). Hooray!
pyshp comes with the shapefile library, which reads a shapefile into a structure of lists and dictionaries.
What should I read? The NC Board of Elections has a shapefile that has all of the voting districts available on their FTP site.
And after downloading, we can read it like so:
import shapefile
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.
To figure out the center of the shapefile, I hope this isn't too simple:
What I should do is get the min/max for both the x and y, then average that. I'll put it all in a list.
coords = list()
for x in range(0,len(shapes)):
xmin = 10000000
xmax = -10000000
ymin = 10000000
ymax = -10000000
for y in range(0,len(shapes[x].points)):
xmin = min(xmin, shapes[x].points[y][0])
xmax = max(xmax, shapes[x].points[y][0])
ymin = min(ymin, shapes[x].points[y][1])
ymax = max(ymax, shapes[x].points[y][1])
coords.append([xmin, xmax,(xmin + xmax)/2, ymin,ymax, (ymin + ymax)/2])
If I want the metadata for each shape through the records method, this is how to do that.
recs = vote.records()
For now, I just care about the shapes and their distance relative to one another.
Pyshp installs through pip without issue (as long as I do it as an admin). Hooray!
pyshp comes with the shapefile library, which reads a shapefile into a structure of lists and dictionaries.
What should I read? The NC Board of Elections has a shapefile that has all of the voting districts available on their FTP site.
And after downloading, we can read it like so:
import shapefile
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.
To figure out the center of the shapefile, I hope this isn't too simple:
What I should do is get the min/max for both the x and y, then average that. I'll put it all in a list.
coords = list()
for x in range(0,len(shapes)):
xmin = 10000000
xmax = -10000000
ymin = 10000000
ymax = -10000000
for y in range(0,len(shapes[x].points)):
xmin = min(xmin, shapes[x].points[y][0])
xmax = max(xmax, shapes[x].points[y][0])
ymin = min(ymin, shapes[x].points[y][1])
ymax = max(ymax, shapes[x].points[y][1])
coords.append([xmin, xmax,(xmin + xmax)/2, ymin,ymax, (ymin + ymax)/2])
If I want the metadata for each shape through the records method, this is how to do that.
recs = vote.records()
For now, I just care about the shapes and their distance relative to one another.
Sunday, January 1, 2017
Voting Districts Day 2: Wrong Python Packages?
I think that I'm going to switch up and use some different packages for reading this spatial data. In order to use the SciPy stuff, I still need more packages and those packages require some unorthodox installation methods. It includes a ton of stuff I'm not sure I need. So, I'm going to do this one package at a time. Well, two in this case.
It looks like Shapely and Fiona can do what I want initially, which is to read shapefiles and plot them.
When I try to use pip for the install ("pip install Fiona"), it doesn't work. It says I need Microsoft Visual C++.
So, after searching the internet, I discovered that I have to install the packages via a wheel. A plethora of these wheels can be found here at this University of California - Irvine website here.
You also use pip to install these wheels; you just have to download them into the same folder first. Also, I have to run the command prompt as an administrator via a right click when starting the application.
Shapely seemed to work like a charm when attempting the old "import shapely" line in Python. Fiona had other ideas.
Looking at Fiona's documentation, she requires a GDAL package, which was also available on the same UC Irvine page that had the other wheels. I installed that one and was finally able to run "import Fiona" successfully.
So, next time, we'll see about reading the shapefiles.
It looks like Shapely and Fiona can do what I want initially, which is to read shapefiles and plot them.
When I try to use pip for the install ("pip install Fiona"), it doesn't work. It says I need Microsoft Visual C++.
So, after searching the internet, I discovered that I have to install the packages via a wheel. A plethora of these wheels can be found here at this University of California - Irvine website here.
You also use pip to install these wheels; you just have to download them into the same folder first. Also, I have to run the command prompt as an administrator via a right click when starting the application.
Shapely seemed to work like a charm when attempting the old "import shapely" line in Python. Fiona had other ideas.
Looking at Fiona's documentation, she requires a GDAL package, which was also available on the same UC Irvine page that had the other wheels. I installed that one and was finally able to run "import Fiona" successfully.
So, next time, we'll see about reading the shapefiles.
Subscribe to:
Posts (Atom)