Skip to content

Instantly share code, notes, and snippets.

@chomy
Created December 7, 2012 07:47
Show Gist options
  • Save chomy/4231552 to your computer and use it in GitHub Desktop.
Save chomy/4231552 to your computer and use it in GitHub Desktop.
Convert JPS to ESRI Grid format
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xml.parsers.expat
import codecs
import sys
nextaction = None
current = None
last = None
west = None
east = None
south= None
north= None
low = None
high = None
fd = None
def start_element(name, attr):
global nextaction, current, last
last = current
current = name
if name == "alti":
nextaction = alti
elif name == "jps:westBoundLongitude":
nextaction = onwest
elif name == "jps:eastBoundLongitude":
nextaction = oneast
elif name == "jps:southBoundLatitude":
nextaction = onsouth
elif name == "jps:northBoundLatitude":
nextaction = onnorth
elif name == "jps:coordValues":
nextaction = onCoordValues
elif name == "mesh":
nextaction = mesh
def end_element(name):
global nextaction
nextaction = None
if name == "jps:extent":
extent()
def text(val):
if nextaction != None:
nextaction(val)
def alti(val):
fd.write("%s " % val)
def onwest(val):
global west
west = float(val)
def oneast(val):
global east
east = float(val)
def onsouth(val):
global south
south = float(val)
def onnorth(val):
global north
north = float(val)
def onCoordValues(val):
global high, low
if last == "jps:high":
high = val.split(" ")
elif last == "jps:low":
low = val.split(" ")
def mesh(val):
global fd
filename = "%s.asc" % val
fd = open(filename, 'w')
print filename
def extent():
ncols = int(high[0]) - int(low[0]) + 1
nrows = int(high[1]) - int(low[1]) + 1
cellsize = (east - west)/ncols
fd.write("ncols %d\n" % ncols)
fd.write("nrows %d\n" % nrows)
fd.write("xllcorner %f\n" % west)
fd.write("yllcorner %f\n" % south)
fd.write("cellsize %s\n" %cellsize)
fd.write("NODATA_value -9999.00\n")
def main():
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = text
p.ParseFile(sys.stdin)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment