Created
January 15, 2012 22:53
-
-
Save hanksims/1617855 to your computer and use it in GitHub Desktop.
Port Distance Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Great circle distance calculations between selected Asian ports and selected ports on the West Coast of | |
# the Americas. | |
# | |
# Using GeoDjango ORM to interact with PostGIS geography columns. | |
# | |
# https://docs.djangoproject.com/en/dev/ref/contrib/gis/ | |
# http://postgis.refractions.net/docs/ch04.html#PostGIS_Geography |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.gis.db import models | |
class Port(models.Model): | |
name = models.CharField(max_length=100) | |
location = models.PointField(geography=True) | |
objects = models.GeoManager() | |
class AsiaPort(Port): | |
objects = models.GeoManager() | |
class AmericaPort(Port): | |
objects = models.GeoManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ports.models import * | |
for asia_port in AsiaPort.objects.all(): | |
targets = AmericaPort.objects.all().distance(asia_port.location).order_by('distance') | |
print('') | |
print(asia_port.name.upper()) | |
print('==============') | |
for target in targets: | |
print('%s: %s miles' % (target.name, int(target.distance.mi)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ports.models import AsiaPort, AmericaPort | |
from django.contrib.gis.geos import Point | |
ASIA_POINTS = { | |
'Busan': Point(129.075556, 35.179444, srid=4326), | |
'Kobe': Point(135.2, 34.683333, srid=4326), | |
'Tokyo': Point(139.75, 35.666667, srid=4326), | |
'Shenzen': Point(113.883333, 22.5, srid=4326), | |
'Shanghai': Point(121.5, 31.2, srid=4326), | |
'Hong Kong': Point(114.166667, 22.25, srid=4326), | |
'Tianjin': Point(117.785278, 38.976667, srid=4326), | |
'Jakarta': Point(106.75, -6.133333, srid=4326), | |
'Taipei': Point(121.633333, 25.033333, srid=4326), | |
'Ho Chi Minh City': Point(106.681944, 10.769444, srid=4326), | |
} | |
AMERICAS_POINTS = { | |
'Humboldt Bay': Point(-124.163611, 40.801944, srid=4326), | |
'Tacoma': Point(-122.408333, 47.260278, srid=4326), | |
'Oakland': Point(-122.284603, 37.795533, srid=4326), | |
'Long Beach': Point(-118.216458, 33.754185, srid=4326), | |
'Prince Rupert': Point(-130.327083, 54.312194, srid=4326), | |
'Ensenada': Point(-116.626389, 31.850833, srid=4326), | |
} | |
AsiaPort.objects.all().delete() | |
AmericaPort.objects.all().delete() | |
for key, value in ASIA_POINTS.items(): | |
port = AsiaPort( | |
name=key, | |
location=value | |
).save() | |
for key, value in AMERICAS_POINTS.items(): | |
port = AmericaPort( | |
name=key, | |
location=value | |
).save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HO CHI MINH CITY | |
============== | |
Prince Rupert: 6862 miles | |
Tacoma: 7430 miles | |
Humboldt Bay: 7632 miles | |
Oakland: 7835 miles | |
Long Beach: 8186 miles | |
Ensenada: 8340 miles | |
JAKARTA | |
============== | |
Prince Rupert: 7857 miles | |
Tacoma: 8385 miles | |
Humboldt Bay: 8501 miles | |
Oakland: 8675 miles | |
Long Beach: 8995 miles | |
Ensenada: 9132 miles | |
SHENZEN | |
============== | |
Prince Rupert: 5926 miles | |
Tacoma: 6494 miles | |
Humboldt Bay: 6706 miles | |
Oakland: 6914 miles | |
Long Beach: 7268 miles | |
Ensenada: 7424 miles | |
BUSAN | |
============== | |
Prince Rupert: 4685 miles | |
Tacoma: 5243 miles | |
Humboldt Bay: 5441 miles | |
Oakland: 5648 miles | |
Long Beach: 6002 miles | |
Ensenada: 6158 miles | |
TOKYO | |
============== | |
Prince Rupert: 4272 miles | |
Tacoma: 4801 miles | |
Humboldt Bay: 4958 miles | |
Oakland: 5154 miles | |
Long Beach: 5501 miles | |
Ensenada: 5653 miles | |
TIANJIN | |
============== | |
Prince Rupert: 4850 miles | |
Tacoma: 5436 miles | |
Humboldt Bay: 5698 miles | |
Oakland: 5919 miles | |
Long Beach: 6279 miles | |
Ensenada: 6439 miles | |
KOBE | |
============== | |
Prince Rupert: 4492 miles | |
Tacoma: 5031 miles | |
Humboldt Bay: 5201 miles | |
Oakland: 5401 miles | |
Long Beach: 5750 miles | |
Ensenada: 5903 miles | |
SHANGHAI | |
============== | |
Prince Rupert: 5167 miles | |
Tacoma: 5734 miles | |
Humboldt Bay: 5947 miles | |
Oakland: 6156 miles | |
Long Beach: 6511 miles | |
Ensenada: 6668 miles | |
HONG KONG | |
============== | |
Prince Rupert: 5931 miles | |
Tacoma: 6498 miles | |
Humboldt Bay: 6707 miles | |
Oakland: 6915 miles | |
Long Beach: 7268 miles | |
Ensenada: 7424 miles | |
TAIPEI | |
============== | |
Prince Rupert: 5512 miles | |
Tacoma: 6065 miles | |
Humboldt Bay: 6248 miles | |
Oakland: 6449 miles | |
Long Beach: 6799 miles | |
Ensenada: 6953 miles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment