#!/usr/bin/env python
"""
shapefile to virtual earth
a completely standalone html generator for generating a web map based on a
set of points
"""
try:
from osgeo import ogr
except ImportError:
import ogr
import sys
template = """
Loading Map
%s
"""
feature_template = '{"lat": %f, "lon":%f, "label":"%s", "info":"%s"}'
def shp2mapstraction(shp, out, labelatt, provider):
if provider.lower() == "ms":
provider_js = ""
provider_init = "var mapstraction = new Mapstraction('mapstraction','microsoft');"
elif provider.lower() == "yahoo":
provider_js = ""
provider_init = "var mapstraction = new Mapstraction('mapstraction','yahoo');"
elif provider.lower() == "openlayers":
provider_js = ""
provider_init = "var mapstraction = new Mapstraction('mapstraction','openlayers');"
else:
print "Choose either Yahoo, OpenLayers or MS as the map service provider"
sys.exit(1)
ds = ogr.Open(shp)
layer = ds.GetLayer(0)
geomtype = layer.GetLayerDefn().GetGeomType()
if geomtype != ogr.wkbPoint and geomtype != ogr.wkbPoint25D:
print " That's not a point shapefile. Thanks for trying. Please play again."
sys.exit(1)
ext = layer.GetExtent()
(clon, clat) = ( (ext[0] + ext[1])/2. , (ext[2] + ext[3])/2. )
fc = layer.GetFeatureCount()
jsfeatures = []
for i in range(fc):
f = layer.GetFeature(i)
g = f.GetGeometryRef()
if labelatt:
fidx = f.GetFieldIndex(labelatt)
label = f.GetFieldAsString(fidx)
else:
label = "point %d" % i
fcount = f.GetFieldCount()
info = ""
for j in range(fcount):
fname = f.GetDefnRef().GetFieldDefn(j).GetName()
val = f.GetFieldAsString(j)
info += "| %s | %s |
" % (fname, val)
info += "
";
jf = (g.GetY(), g.GetX(), label , info )
jsfeatures.append(jf)
points = ", ".join( [feature_template % x for x in jsfeatures])
# should we display labels or not?
if labelatt:
jslabel = "my_marker.setLabel(points[i]['label']);"
else:
jslabel = "//my_marker.setLabel(points[i]['label']);"
html = template % (provider_js, provider_init, clat, clon, points, jslabel)
fh = open(out,"w")
fh.write(html)
fh.close()
return True
if __name__ == "__main__":
if len(sys.argv) < 4:
print "shp2Mapstraction.py