[OpenLayers-Commits] r5634 - sandbox/rdewit/kml/examples

commits at openlayers.org commits at openlayers.org
Thu Jan 3 00:59:38 EST 2008


Author: rdewit
Date: 2008-01-03 00:59:38 -0500 (Thu, 03 Jan 2008)
New Revision: 5634

Modified:
   sandbox/rdewit/kml/examples/proxy.cgi
Log:
Experimental support for KMZ files


Modified: sandbox/rdewit/kml/examples/proxy.cgi
===================================================================
--- sandbox/rdewit/kml/examples/proxy.cgi	2008-01-03 05:52:37 UTC (rev 5633)
+++ sandbox/rdewit/kml/examples/proxy.cgi	2008-01-03 05:59:38 UTC (rev 5634)
@@ -12,9 +12,12 @@
 import urllib2
 import cgi
 import sys, os
+import zipfile, re, StringIO #for KMZ support
 
+
 # Designed to prevent Open Proxy type stuff.
 
+
 allowedHosts = ['www.openlayers.org', 'openlayers.org', 
                 'labs.metacarta.com', 'world.freemap.in', 
                 'prototype.openmnnd.org', 'geo.openplans.org',
@@ -36,7 +39,9 @@
 
 try:
     host = url.split("/")[2]
-    if allowedHosts and not host in allowedHosts:
+    #isLocalHost = (os.environ["SERVER_NAME"] == "localhost") or (os.environ["HTTP_HOST"] == "localhost")
+    isLocalHost = False
+    if not isLocalHost and allowedHosts and not host in allowedHosts:
         print "Status: 502 Bad Gateway"
         print "Content-Type: text/plain"
         print
@@ -62,9 +67,30 @@
         else:
             print "Content-Type: text/plain"
         print
+
+        # Handle KMZ files
+        # First put content in StringIO object, so ZipFile can handle it
+        fileObject = StringIO.StringIO()
+        fileObject.write(y.read())
+
+        # Now try to handle it as a ZIP file
+        try:
+            zipObject = zipfile.ZipFile(fileObject, "r")
+
+        # if not, print the contents (most cases)
+        except zipfile.BadZipfile:
+            fileObject.seek(0) # reset pointer to start of file
+            print fileObject.read()
+
+        # It *is* a ZIP! Now loop through all files in ZIP
+        # and only print the ones ending in .kml
+        else:
+            for info in zipObject.infolist():
+                if re.search(r'''\.kml$''', info.filename):
+                  print zipObject.read(info.filename)
+            zipObject.close()
         
-        print y.read()
-        
+        fileObject.close()
         y.close()
     else:
         print "Content-Type: text/plain"



More information about the Commits mailing list