Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunghao Huang authored Jan 15, 2018
1 parent 929d81b commit 20c2193
Show file tree
Hide file tree
Showing 4 changed files with 4,002 additions and 0 deletions.
73 changes: 73 additions & 0 deletions DistanceCalculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import gdal, osr
from skimage.graph import route_through_array
import numpy as np
from geopy.distance import vincenty
import matplotlib.pyplot as plt
import pandas as pd

#Transforming a raster map to array datatype.
raster = gdal.Open('/Users/j22700126/Desktop/Project Seminar/Distance Calculator/map.tif')
band = raster.GetRasterBand(1)
mapArray = band.ReadAsArray()

#Get geotransform information and declare some variables for later use
geotransform = raster.GetGeoTransform()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]

ports = pd.read_csv('/Users/j22700126/Desktop/Project Seminar/Distance Calculator/port.csv')

#Visualize the map base on the array, if you want, not neccessary.
plt.imshow(mapArray)
plt.gray()
plt.show()

#transform the coordinates to the exact position in the array.
def coord2pixelOffset(x,y):

xOffset = int((x - originX)/pixelWidth)
yOffset = int((y - originY)/pixelHeight)
return xOffset,yOffset

#create a path which travels through the cost map.
def createPath(costSurfaceArray,startCoord,stopCoord):

# coordinates to array index
startCoordX = startCoord[0]
startCoordY = startCoord[1]
startIndexX,startIndexY = coord2pixelOffset(startCoordX,startCoordY)

stopCoordX = stopCoord[0]
stopCoordY = stopCoord[1]
stopIndexX,stopIndexY = coord2pixelOffset(stopCoordX,stopCoordY)

# create path
indices, weight = route_through_array(costSurfaceArray, (startIndexY,startIndexX), (stopIndexY,stopIndexX),geometric=True,fully_connected=True)
indices = np.array(indices).T
indices = indices.astype(float)
indices[1] = indices[1]*pixelWidth + originX
indices[0] = indices[0]*pixelHeight + originY
return indices

#Calculate the vincenty distance starts from the first pair of points to the last.
def calculateDistance(pathIndices):
distance = 0
for i in range(0,(len(pathIndices[0])-1)):
distance += vincenty((pathIndices[1,i], pathIndices[0,i]), (pathIndices[1,i+1], pathIndices[0,i+1])).miles*0.868976
return distance


def distanceCalculator(startCoord, stopCoord):
pathIndices = createPath(mapArray,startCoord,stopCoord)
distance = calculateDistance(pathIndices)
print(distance)
return distance

ports.Port[1000]

startCoord = (ports.longitude[2205], ports.latitude[2205])
stopCoord = (ports.longitude[1000], ports.latitude[100])

distanceCalculator(startCoord, stopCoord)
Binary file added map.tif
Binary file not shown.
259 changes: 259 additions & 0 deletions map.tif.aux.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
<PAMDataset>
<PAMRasterBand band="1">
<CategoryNames>
<Category></Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>5</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>4</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>3</Category>
<Category>5</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>5</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>4</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>3</Category>
<Category>1</Category>
<Category>6</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>5</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>5</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>3</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>6</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>3</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category>1</Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
<Category></Category>
</CategoryNames>
</PAMRasterBand>
</PAMDataset>
Loading

0 comments on commit 20c2193

Please sign in to comment.