added folkstone app
This commit is contained in:
parent
ff5b810cc0
commit
e52916d8f8
0
ais/folkstone/__init__.py
Normal file
0
ais/folkstone/__init__.py
Normal file
15
ais/folkstone/models.py
Normal file
15
ais/folkstone/models.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
from django.contrib.gis.db import models
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
class Ship(models.Model):
|
||||||
|
# point = models.PointField()
|
||||||
|
name = models.CharField(max_length=255)
|
||||||
|
mmsi = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
class Position(models.Model):
|
||||||
|
ship = models.ForeignKey(Ship)
|
||||||
|
point = models.PointField()
|
||||||
|
timestamp = models.DateTimeField(default=datetime.datetime.now())
|
||||||
|
objects = models.GeoManager()
|
||||||
|
|
||||||
|
# Create your models here.
|
47
ais/folkstone/parse.py
Normal file
47
ais/folkstone/parse.py
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import oxweb
|
||||||
|
import oxlib
|
||||||
|
import urllib2
|
||||||
|
import json
|
||||||
|
from django.contrib.gis.geos import Point
|
||||||
|
|
||||||
|
|
||||||
|
BASE_URL = "http://localhost:8000/shipjson"
|
||||||
|
|
||||||
|
def json_request(url):
|
||||||
|
try:
|
||||||
|
# request = urllib2.Request(url)
|
||||||
|
# request.add_header('User-agent', 'oxclient')
|
||||||
|
# body = str(form)
|
||||||
|
# request.add_header('Content-type', form.get_content_type())
|
||||||
|
# request.add_header('Content-length', len(body))
|
||||||
|
# request.add_data(body)
|
||||||
|
result = urllib2.urlopen(url).read().strip()
|
||||||
|
return json.loads(result)
|
||||||
|
except urllib2.HTTPError, e:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
if (len(sys.argv)) < 5:
|
||||||
|
print "usage: %s sw_x sw_y ne_x ne_y\n" % (sys.argv[0],)
|
||||||
|
print "example: %s 23.0 36.6 26.8 38.00000000006" % (sys.argv[0])
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
from folkstone.models import *
|
||||||
|
args = sys.argv
|
||||||
|
url = "%s?sw_x=%s&sw_y=%s&ne_x=%s&ne_y=%s" % (BASE_URL, args[1], args[2], args[3], args[4],)
|
||||||
|
print url
|
||||||
|
geojson = json_request(url)
|
||||||
|
for f in geojson['features']:
|
||||||
|
mmsi = f['properties']['mmsi']
|
||||||
|
name = f['properties']['name']
|
||||||
|
point = Point(float(f['geometry']['coordinates'][0]), float(f['geometry']['coordinates'][1]))
|
||||||
|
if Ship.objects.filter(mmsi=mmsi).count() > 1:
|
||||||
|
ship = Ship.objects.get(mmsi=mmsi)
|
||||||
|
else:
|
||||||
|
ship = Ship(mmsi=mmsi, name=name)
|
||||||
|
p = Position(ship=ship, point=point)
|
||||||
|
print p.timestamp
|
||||||
|
# features = json.loads(geojson)
|
||||||
|
# print features
|
||||||
|
# url = "http://marinetraffic.com/ais/getxml.aspx?id=0.8963493388008952&sw_x=%s&sw_y=%s&ne_x=%s&ne_y=%s&zoom=10&fleet=" % (args[1], args[2], args[3], args[4])
|
23
ais/folkstone/tests.py
Normal file
23
ais/folkstone/tests.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
"""
|
||||||
|
This file demonstrates two different styles of tests (one doctest and one
|
||||||
|
unittest). These will both pass when you run "manage.py test".
|
||||||
|
|
||||||
|
Replace these with more appropriate tests for your application.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
class SimpleTest(TestCase):
|
||||||
|
def test_basic_addition(self):
|
||||||
|
"""
|
||||||
|
Tests that 1 + 1 always equals 2.
|
||||||
|
"""
|
||||||
|
self.failUnlessEqual(1 + 1, 2)
|
||||||
|
|
||||||
|
__test__ = {"doctest": """
|
||||||
|
Another way to test that 1 + 1 is equal to 2.
|
||||||
|
|
||||||
|
>>> 1 + 1 == 2
|
||||||
|
True
|
||||||
|
"""}
|
||||||
|
|
40
ais/folkstone/views.py
Normal file
40
ais/folkstone/views.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# Create your views here.
|
||||||
|
from django.shortcuts import render_to_response
|
||||||
|
import urllib2
|
||||||
|
from oxdjango.shortcuts import render_to_json_response
|
||||||
|
import elementtree.ElementTree as ET
|
||||||
|
|
||||||
|
def index(request):
|
||||||
|
return render_to_response("folkstone.html")
|
||||||
|
|
||||||
|
def shipjson(request):
|
||||||
|
ext = request.GET
|
||||||
|
url = "http://marinetraffic.com/ais/getxml.aspx?id=0.8963493388008952&sw_x=%s&sw_y=%s&ne_x=%s&ne_y=%s&zoom=10&fleet=" % (ext['sw_x'], ext['sw_y'], ext['ne_x'], ext['ne_y'])
|
||||||
|
# print url
|
||||||
|
u = urllib2.urlopen(url)
|
||||||
|
xml = u.read()
|
||||||
|
u.close()
|
||||||
|
tree = ET.XML(xml)
|
||||||
|
arr = {
|
||||||
|
'type': 'FeatureCollection',
|
||||||
|
'features': []
|
||||||
|
}
|
||||||
|
for ship in tree.findall("V_POS"):
|
||||||
|
lon = ship.attrib['LON']
|
||||||
|
lat = ship.attrib['LAT']
|
||||||
|
arr['features'].append({
|
||||||
|
'type': 'Feature',
|
||||||
|
'geometry': {'type': 'Point', 'coordinates': [float(lon), float(lat)]},
|
||||||
|
'properties': {'name': ship.attrib['N'], 'mmsi': ship.attrib['M']}
|
||||||
|
})
|
||||||
|
return render_to_json_response(arr)
|
||||||
|
|
||||||
|
def shipDetail(request):
|
||||||
|
mmsi = request.GET['mmsi']
|
||||||
|
name = request.GET['name']
|
||||||
|
MT_Basic_Url = "http://marinetraffic.com/ais/shipinfo.aspx?mmsi=%s&header=true&id=0.6916635176281586" % (mmsi,)
|
||||||
|
u = urllib2.urlopen(MT_Basic_Url)
|
||||||
|
html = u.read()
|
||||||
|
return render_to_json_response({
|
||||||
|
'MT_Basic': html
|
||||||
|
})
|
|
@ -1,3 +1,5 @@
|
||||||
-e svn+http://code.djangoproject.com/svn/django/branches/releases/1.1.X/#egg=django
|
-e svn+http://code.djangoproject.com/svn/django/trunk/#egg=django
|
||||||
-e bzr+http://code.0xdb.org/python-oxdjango/#egg=python-oxdjango
|
-e bzr+http://code.0xdb.org/python-oxdjango/#egg=python-oxdjango
|
||||||
|
-e bzr+http://code.0xdb.org/python-oxlib/#egg=python-oxlib
|
||||||
|
-e bzr+http://code.0xdb.org/python-oxweb/#egg=python-oxweb
|
||||||
South
|
South
|
||||||
|
|
Loading…
Reference in New Issue
Block a user