From e52916d8f8e1695717795df970c67cba78f0fe82 Mon Sep 17 00:00:00 2001 From: sanj Date: Mon, 10 May 2010 17:58:23 +0530 Subject: [PATCH] added folkstone app --- ais/folkstone/__init__.py | 0 ais/folkstone/models.py | 15 +++++++++++++ ais/folkstone/parse.py | 47 +++++++++++++++++++++++++++++++++++++++ ais/folkstone/tests.py | 23 +++++++++++++++++++ ais/folkstone/views.py | 40 +++++++++++++++++++++++++++++++++ requirements.txt | 4 +++- 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 ais/folkstone/__init__.py create mode 100644 ais/folkstone/models.py create mode 100644 ais/folkstone/parse.py create mode 100644 ais/folkstone/tests.py create mode 100644 ais/folkstone/views.py diff --git a/ais/folkstone/__init__.py b/ais/folkstone/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ais/folkstone/models.py b/ais/folkstone/models.py new file mode 100644 index 0000000..cce2d85 --- /dev/null +++ b/ais/folkstone/models.py @@ -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. diff --git a/ais/folkstone/parse.py b/ais/folkstone/parse.py new file mode 100644 index 0000000..96673d3 --- /dev/null +++ b/ais/folkstone/parse.py @@ -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]) diff --git a/ais/folkstone/tests.py b/ais/folkstone/tests.py new file mode 100644 index 0000000..2247054 --- /dev/null +++ b/ais/folkstone/tests.py @@ -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 +"""} + diff --git a/ais/folkstone/views.py b/ais/folkstone/views.py new file mode 100644 index 0000000..65fa5ab --- /dev/null +++ b/ais/folkstone/views.py @@ -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 + }) diff --git a/requirements.txt b/requirements.txt index b139f49..f1add57 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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-oxlib/#egg=python-oxlib +-e bzr+http://code.0xdb.org/python-oxweb/#egg=python-oxweb South