Commits

Shu Zong Chen committed 19e2e6d

Saving work

Comments (0)

Files changed (17)

 syntax: glob
 *.pyc
+*.swp
 *.sqlite3
-
+localsettings.py
+upstreams/

cjh/__init__.py

Empty file added.

cjh/gis_io/__init__.py

Empty file added.

cjh/gis_io/management/__init__.py

Empty file added.

cjh/gis_io/management/commands/__init__.py

Empty file added.

cjh/gis_io/management/commands/gis_convert_to_kml.py

+from django.core.management.base import BaseCommand, CommandError
+from cjh.gis_io.models import GIS_IO
+import glob
+import os
+
+class Command(BaseCommand):
+	args = '<none>'
+	help = 'Download all gis information locally'
+
+	def handle(self, *args, **kwargs):
+		streams = GIS_IO.upstreams()
+		for stream in streams:
+			for fn in glob.glob(os.path.join(stream.download_path, '*.shp')):
+				os.system('ogr2ogr -f "KML" {fn}.kml {fn}'.format(fn=fn))
+				self.stdout.write('Finished {fn}\n'.format(fn=fn))

cjh/gis_io/management/commands/gis_download.py

+from django.core.management.base import BaseCommand, CommandError
+from cjh.gis_io.models import GIS_IO
+import os
+import urllib2
+
+class Command(BaseCommand):
+	args = '<none>'
+	help = 'Download all gis information locally'
+
+	def handle(self, *args, **kwargs):
+		streams = GIS_IO.upstreams()
+		for stream in streams:
+			try:
+				os.makedirs(stream.download_path)
+			except OSError:
+				pass
+			print "opening", stream.url
+			remote_fh = urllib2.urlopen(stream.url)
+			filen = os.path.join(stream.download_path, 'upstream.zip')
+			with open(filen, 'w') as fh:
+				fh.write(remote_fh.read())
+			os.system('unzip {fn} -d {path}'.format(fn=filen, path=stream.download_path))
+		#self.stdout.write(repr(streams))

cjh/gis_io/management/commands/gis_publish.py

+from django.core.management.base import BaseCommand, CommandError
+from cjh.gis_io.models import GIS_IO
+import glob
+import os
+import lxml.etree
+
+class Command(BaseCommand):
+	args = '<none>'
+	help = 'Download all gis information locally'
+
+	def handle(self, *args, **kwargs):
+		streams = GIS_IO.upstreams()
+		for stream in streams:
+			for fn in glob.glob(os.path.join(stream.download_path, '*.kml')):
+				with open(fn) as fh:
+					kml = lxml.etree.parse(fh)
+				items = kml.findall(stream.xpath['query'], namespaces=stream.xpath['namespace'])
+				for i in items:
+					meta = i.find(stream.xpath['meta'], namespaces=stream.xpath['namespace'])
+					shape = i.find(stream.xpath['shape'], namespaces=stream.xpath['namespace'])
+					shape_text = lxml.etree.tostring(shape)
+				#kml.write(self.stdout)
+				#print lxml.etree.tostring(kml)

cjh/gis_io/models.py

+from django.db import models
+from django.conf import settings
+import os.path
+import yaml
+
+DL_YAML = os.path.join(settings.BASE_PATH, '../upstreams.yaml')
+DL_BASE = os.path.join(settings.BASE_PATH, '../upstreams')
+
+class GIS_IO(object):
+	_upstreams = None
+
+	@staticmethod
+	def get_upstreams():
+		with open(DL_YAML) as fh:
+			data = yaml.load(fh)
+		ret = []
+		for d in data['data']:
+			for s in d['sources']:
+				ret.append(GIS_Upstream(d['id'], d['name'], s))
+		return ret
+
+	@staticmethod
+	def upstreams():
+		if GIS_IO._upstreams is None:
+			GIS_IO._upstreams = GIS_IO.get_upstreams()
+		return GIS_IO._upstreams
+
+class GIS_Upstream(object):
+	def __init__(self, parent_id, parent_name, data):
+		self.parent_id = parent_id
+		self.parent_name = parent_name
+		self.id = data['id']
+		self.name = data['name']
+		self.url = data['url']
+		self.xpath = data['xpath']
+
+	def __str__(self):
+		return "<{id}: {url}>".format(id=self.id, url=self.url)
+	
+	@property
+	def download_path(self):
+		return os.path.normpath(os.path.join(DL_BASE, self.parent_id, self.id))
+

cjh/gis_io/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this 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.assertEqual(1 + 1, 2)

cjh/gis_io/views.py

+# Create your views here.

cjh/localsettings.py.sample

+GOOGLE_LOGIN = "login"
+GOOGLE_PASSWORD = "password"
+
+#!/usr/bin/env python
+from django.core.management import execute_manager
+import imp
+try:
+    imp.find_module('settings') # Assumed to be in the same directory.
+except ImportError:
+    import sys
+    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
+    sys.exit(1)
+
+import settings
+
+if __name__ == "__main__":
+    execute_manager(settings)
+# Django settings for cjh project.
+import os.path
+
+BASE_PATH = os.path.abspath(os.path.dirname(__file__))
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+ADMINS = (
+    # ('Your Name', 'your_email@example.com'),
+)
+
+MANAGERS = ADMINS
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+        'NAME': '',                      # Or path to database file if using sqlite3.
+        'USER': '',                      # Not used with sqlite3.
+        'PASSWORD': '',                  # Not used with sqlite3.
+        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
+        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
+    }
+}
+
+# Local time zone for this installation. Choices can be found here:
+# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
+# although not all choices may be available on all operating systems.
+# On Unix systems, a value of None will cause Django to use the same
+# timezone as the operating system.
+# If running in a Windows environment this must be set to the same as your
+# system time zone.
+TIME_ZONE = 'America/Chicago'
+
+# Language code for this installation. All choices can be found here:
+# http://www.i18nguy.com/unicode/language-identifiers.html
+LANGUAGE_CODE = 'en-us'
+
+SITE_ID = 1
+
+# If you set this to False, Django will make some optimizations so as not
+# to load the internationalization machinery.
+USE_I18N = True
+
+# If you set this to False, Django will not format dates, numbers and
+# calendars according to the current locale
+USE_L10N = True
+
+# Absolute filesystem path to the directory that will hold user-uploaded files.
+# Example: "/home/media/media.lawrence.com/media/"
+MEDIA_ROOT = ''
+
+# URL that handles the media served from MEDIA_ROOT. Make sure to use a
+# trailing slash.
+# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
+MEDIA_URL = ''
+
+# Absolute path to the directory static files should be collected to.
+# Don't put anything in this directory yourself; store your static files
+# in apps' "static/" subdirectories and in STATICFILES_DIRS.
+# Example: "/home/media/media.lawrence.com/static/"
+STATIC_ROOT = ''
+
+# URL prefix for static files.
+# Example: "http://media.lawrence.com/static/"
+STATIC_URL = '/static/'
+
+# URL prefix for admin static files -- CSS, JavaScript and images.
+# Make sure to use a trailing slash.
+# Examples: "http://foo.com/static/admin/", "/static/admin/".
+ADMIN_MEDIA_PREFIX = '/static/admin/'
+
+# Additional locations of static files
+STATICFILES_DIRS = (
+    # Put strings here, like "/home/html/static" or "C:/www/django/static".
+    # Always use forward slashes, even on Windows.
+    # Don't forget to use absolute paths, not relative paths.
+)
+
+# List of finder classes that know how to find static files in
+# various locations.
+STATICFILES_FINDERS = (
+    'django.contrib.staticfiles.finders.FileSystemFinder',
+    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
+#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
+)
+
+# Make this unique, and don't share it with anybody.
+SECRET_KEY = '=r_sqd6ak)=)*q%oag%59!^$nfu_cepxumt**hd$dgrfv19nx+'
+
+# List of callables that know how to import templates from various sources.
+TEMPLATE_LOADERS = (
+    'django.template.loaders.filesystem.Loader',
+    'django.template.loaders.app_directories.Loader',
+#     'django.template.loaders.eggs.Loader',
+)
+
+MIDDLEWARE_CLASSES = (
+    'django.middleware.common.CommonMiddleware',
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.middleware.csrf.CsrfViewMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+    'django.contrib.messages.middleware.MessageMiddleware',
+)
+
+ROOT_URLCONF = 'cjh.urls'
+
+TEMPLATE_DIRS = (
+    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
+    # Always use forward slashes, even on Windows.
+    # Don't forget to use absolute paths, not relative paths.
+)
+
+INSTALLED_APPS = (
+    'django.contrib.auth',
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'django.contrib.sites',
+    'django.contrib.messages',
+    'django.contrib.staticfiles',
+    'django.contrib.admin',
+		'cjh.gis_io',
+)
+
+# A sample logging configuration. The only tangible logging
+# performed by this configuration is to send an email to
+# the site admins on every HTTP 500 error.
+# See http://docs.djangoproject.com/en/dev/topics/logging for
+# more details on how to customize your logging configuration.
+LOGGING = {
+    'version': 1,
+    'disable_existing_loggers': False,
+    'handlers': {
+        'mail_admins': {
+            'level': 'ERROR',
+            'class': 'django.utils.log.AdminEmailHandler'
+        }
+    },
+    'loggers': {
+        'django.request': {
+            'handlers': ['mail_admins'],
+            'level': 'ERROR',
+            'propagate': True,
+        },
+    }
+}
+
+from localsettings import *
+from django.conf.urls.defaults import patterns, include, url
+
+# Uncomment the next two lines to enable the admin:
+# from django.contrib import admin
+# admin.autodiscover()
+
+urlpatterns = patterns('',
+    # Examples:
+    # url(r'^$', 'cjh.views.home', name='home'),
+    # url(r'^cjh/', include('cjh.foo.urls')),
+
+    # Uncomment the admin/doc line below to enable admin documentation:
+    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
+
+    # Uncomment the next line to enable the admin:
+    # url(r'^admin/', include(admin.site.urls)),
+)
+Django
+PyYAML
+hg+https://bitbucket.org/michael_kirk/python-fusion-tables-client
+pykml
+data:
+  - id: hicentral
+    name: Hawaii
+    sources:
+      - id: hicentral_flood_zones
+        name: Hawaii Flood Zones
+        url: "ftp://gisftp.hicentral.com/layers/FEMA_Flood_Hydrography/Flood_Zones_2011.zip"
+        xpath:
+          namespace:
+            "ns": "http://www.opengis.net/kml/2.2"
+          query: ".//ns:Placemark"
+          meta: "./ns:ExtendedData"
+          shape: "./ns:Polygon"
+      - id: hicentral_emergency_shelters
+        name: Hawaii Emergency Shelters
+        url: "ftp://gisftp.hicentral.com/layers/Public_Safety/emergency_shelters.zip"
+        xpath:
+          namespace:
+            "ns": "http://www.opengis.net/kml/2.2"
+          query: ".//ns:Placemark"
+          meta: "./ns:ExtendedData"
+          shape: "./ns:Point"
+      - id: hicentral_tsunami_evac_zones
+        name: Hawaii Tsunami Evacuation Zones
+        url: "ftp://gisftp.hicentral.com/layers/Public_Safety/TsunamiEvacuationZone.zip"
+        xpath:
+          namespace:
+            "ns": "http://www.opengis.net/kml/2.2"
+          query: ".//ns:Placemark"
+          meta: "./ns:ExtendedData"
+          shape: "./ns:Polygon"
+      - id: hicentral_area_of_interest
+        name: Hawaii Areas of Interest
+        url: "ftp://gisftp.hicentral.com/layers/Basemap/Area_0f_Interest.zip"
+        xpath:
+          namespace:
+            "ns": "http://www.opengis.net/kml/2.2"
+          query: ".//ns:Placemark"
+          meta: "./ns:ExtendedData"
+          shape: "./ns:Polygon"
+