Created
March 14, 2011 10:00
-
-
Save deleted/868961 to your computer and use it in GitHub Desktop.
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
import sys, os | |
import optparse | |
import glob | |
import json | |
import time | |
import zipfile | |
import kmlengine | |
import kmldom | |
from kmlengine import Bbox | |
DEFAULT_SECTOR_JSONFILE = "sectors.json" | |
def create_zipfile(outZip, files, delete_after=True): | |
zfile = zipfile.Zipfile(outZip) | |
try: | |
for file in files: | |
zfile.write(file) | |
finally: | |
zfile.close() | |
for file in files: | |
os.unlink(file) | |
def bbox_for_sector(sector, sectorfile=options.sector_file): | |
sectors = json.loads(open(sectorfile).read()) | |
props = sectors[sector] | |
north, west, south, east = props['bounds'] | |
bbox = Bbox() | |
bbox.ExpandLatLon(north, west) | |
bbox.ExpandLatLon(south, east) | |
return bbox | |
class KmlFilter(object): | |
def __init__(self, bbox=None): | |
self.bbox = bbox | |
def handleKmlFile(self, url, isTopLevel=False): | |
print 'processing:', url | |
self.urlStack.append(url) | |
tmpPath = self.getLocalPath(url) | |
# Parse with libkml | |
# walk the dom for features | |
# compare each feature's bbox to the filter bbox and accept or reject | |
# if the feature is a NetworkLink (and is not rejected), recurse on it | |
if isTopLevel: | |
outPath = '%s/%s' % (self.outDir, 'doc.kml') | |
else: | |
(fd, outPath) = tempfile.mkstemp('-KmlSnapshot') | |
os.close(fd) | |
SysUtil.chmodIfPossible(outPath, 0777) | |
file(outPath, 'w').write(outStr.encode('utf-8')) | |
self.urlStack.pop() | |
return outPath | |
def spiderKmlFile(self, inUrl, outDir): | |
SysUtil.mkdirP(outDir) | |
self.outDir = outDir | |
self.handleKmlFile(inUrl, True) | |
def start_filter(self, inUrl, outZip=None): | |
if not outZip: | |
timestamp = time.strftime('%Y-%m-%d-%H%Mh') | |
SysUtil.mkdirP('/tmp/kml') | |
outZip = '/tmp/kml/filtered-%s.zip' % timestamp | |
outZip = os.path.abspath(outZipRel) | |
outZipBasename = os.path.basename(outZipRel) | |
outZipNoExt, _ = os.path.splitext(outZipBasename) | |
tempDir = '/tmp/tmpKmlSnapshot-%d-%d' % (os.getpid(), random.randint(0,9999)) | |
tempDirContents = os.path.join(tempDir, outZipNoExt) | |
self.spiderKmlFile(inUrl, tempDirContents) | |
oldDir = os.getcwd() | |
os.chdir(tempDir) | |
files = string.join(glob.glob('*'), ' ') | |
create_zipfile(outZip, files) | |
os.chdir(oldDir) | |
return outZip | |
def start_filter(inurl, outzip=None): | |
bbox = bbox_for_sector(options.sector) | |
return KmlFilter(bbox).start_filter(inurl, outzip) | |
def main(): | |
parser = optparse.OptionParser() | |
parser.add_option('--sector-file', dest=sector_filename, help="File containing JSON representation of the sectors to filter by.", default=DEFAULT_SECTOR_FILE) | |
parser.add_option('--sector', dest=sector, help="Name of the sector to filter by.", default="OFUNTO") | |
global options | |
options, args = parser.parse_args() | |
if len(args) > 2: | |
print 'KmlSnapshot: expected no more than 2 positional arguments' | |
parser.print_help() | |
sys.exit(1) | |
start_filter(*args) | |
if __name__ == "__main__": | |
main(): |
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
{ | |
"KESENNUMA": | |
{ | |
"maxZoom": 18, | |
"minZoom": 2, | |
"name": "KESENNUMA SECTOR", | |
"bounds": [ | |
38.978549999999998, | |
141.48361666666668, | |
38.7759, | |
141.73778333333334 | |
] | |
}, | |
"OFUNTO": | |
{ | |
"maxZoom": 18, | |
"minZoom": 2, | |
"name": "OFUNATO SECTOR", | |
"bounds": [ | |
39.174016666666667, | |
141.66826333512131, | |
38.985799999999998, | |
141.7913534314379 | |
] | |
}, | |
"RIKUZENTAKATA": | |
{ | |
"maxZoom": 18, | |
"minZoom": 2, | |
"name": "RIKUZENTAKATA SECTOR", | |
"bounds": [ | |
39.178766666666668, | |
141.38698333333335, | |
38.985799999999998, | |
141.71613333333335 | |
] | |
}, | |
"SOUTH_KAMAISHI": | |
{ | |
"maxZoom": 18, | |
"minZoom": 2, | |
"name": "SOUTH KAMAISHI SECTOR", | |
"bounds": [ | |
39.174016666666667, | |
141.74863333333334, | |
39.024416666666667, | |
141.93601666666666 | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment