-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-eight.py
65 lines (59 loc) · 3.29 KB
/
test-eight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import math
import csv
#Now I want to write a sample csv file with the distances between a few properties and their closest subway stations.
#Inspired by http://iquantny.tumblr.com/post/99544282749/found-the-manhattan-apartment-thats-the-farthest
#(To roughly replicate their results, set the argument on the final line to a million.)
#For full documentation of the people/threads/tutorials that helped lead to this project, please view the python comments in these scripts:
#https://github.com/patrickmaynard/playing-with-qgis-python
class Snobbery(object):
@staticmethod
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
km = 6367 * c
return km
@staticmethod
def calculateDistance(featureProperty, layerEntrances):
#This is a stub for what will eventually be our distance-to-closest-station function.
shortestDistance = 1000
featuresEntrances = layerEntrances.getFeatures()
for featureEntrance in featuresEntrances:
latEntrance = featureEntrance.geometry().centroid().asPoint().y()
lonEntrance = featureEntrance.geometry().centroid().asPoint().x()
latProperty = featureProperty.geometry().centroid().asPoint().y()
lonProperty = featureProperty.geometry().centroid().asPoint().x()
currentDistance = Snobbery.haversine(lonEntrance, latEntrance, lonProperty, latProperty)
if currentDistance < shortestDistance:
shortestDistance = currentDistance
return shortestDistance
@staticmethod
def importAndAnalyze(rowLimit = 3):
layerProperties = iface.addVectorLayer("/Users/patrickmaynard/Desktop/delete-me-soon/new-crs.shp", "Properties", "ogr")
if not layerProperties:
print "layerProperties failed to load!"
layerEntrances = iface.addVectorLayer("/Users/patrickmaynard/Downloads/subway/geo_export_950003d9-f387-479c-9bf5-a1ca5a252bce.shp", "Entrances", "ogr")
if not layerEntrances:
print "layerProperties failed to load!"
features = layerProperties.getFeatures()
counter = 0
featuresSelected = []
with open('/Users/patrickmaynard/Desktop/eggs.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
spamwriter.writerow(['Address', 'Lat','Lon', 'Distance'])
for feature in features:
if counter < rowLimit:
#print feature['name']
print feature['Address']
spamwriter.writerow([feature['Address'], feature.geometry().centroid().asPoint().y(),feature.geometry().centroid().asPoint().x(), Snobbery.calculateDistance(feature, layerEntrances)])
counter += 1
Snobbery.importAndAnalyze(5)