Hi, I would like to share with the list the following piece of code,
Together with gdal2tiles it allows generating a geojson layers from an html client-side image map, for browsing with openlayers. Usage: gdal2tiles.py test.png test/ map2geojson.py 220.0 520.0 1.0 < test.map > test/polygon.json add in openlayers.html + gml = new OpenLayers.Layer.GML("GeoJSON", "polygon.json", {format: OpenLayers.Format.GeoJSON}); + map.addLayer(gml); + selectFeature = new OpenLayers.Control.SelectFeature(gml, {hover: true}); + map.addControl(selectFeature); + selectFeature.activate(); You can see the result here: http://ligamen.org/work/test/openlayers.html Let me know if it is worth including in gdal2tiles, or if it needs more love ? Thanks. -- Johan Euphrosine <pro...@aminche.com>
#!/usr/bin/env python ############################################################################### # # Purpose: Convert html client-side image map into geojson metadata. # Author: Johan Euphrosine <pro...@aminche.com> # ############################################################################### # Copyright (c) 2009, Johan Euphrosine <pro...@aminche.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Library General Public License for more details. # # You should have received a copy of the GNU Library General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA ############################################################################### import sys import os from lxml import objectify import simplejson as json import unittest def map2geojson(areas, size, scale): w, h = size def to_features(areas): for area in areas: values = [int(c) for c in area.get("coords").split(",")] coords = zip([x*scale for x in values[::2]], [h-(y*scale) for y in values[1::2]]) yield {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [coords]}, "properties": { "name": area.get("title") }} root = objectify.fromstring('<map2geojson>%s</map2geojson>' % areas) return json.dumps({"type": "FeatureCollection", "features": list(to_features(root.area))}) class Map2GeoJsonTestCase(unittest.TestCase): def test(self): map = """<area shape="polygon" title="a" coords="142,200,136,206,124,209,124,209,133,201,142,200" /> <area shape="polygon" title="b" coords="142,200,133,199,123,192,123,192,135,194,142,200" />""" size = (220.0, 520.0) scaled = (3000.0, 7090.0) scale = scaled[0]/size[0] geojson = json.loads(map2geojson(map, scaled, scale)) features = geojson["features"] self.assertEquals("a", features[0]["properties"]["name"]) self.assertEquals([[[1936.3636363636365, 4362.7272727272721], [1854.5454545454545, 4280.909090909091], [1690.909090909091, 4240.0], [1690.909090909091, 4240.0], [1813.6363636363637, 4349.090909090909], [1936.3636363636365, 4362.7272727272721]]], features[0]["geometry"]["coordinates"]) self.assertEquals("b", features[1]["properties"]["name"]) self.assertEquals([[[1936.3636363636365, 4362.7272727272721], [1813.6363636363637, 4376.363636363636], [1677.2727272727273, 4471.818181818182], [1677.2727272727273, 4471.818181818182], [1840.909090909091, 4444.545454545454], [1936.3636363636365, 4362.7272727272721]]], features[1]["geometry"]["coordinates"]) if __name__ == '__main__': if len(sys.argv) == 1: unittest.main() else: print map2geojson(sys.stdin.read(), (float(sys.argv[1]), float(sys.argv[2])), float(sys.argv[3]))
signature.asc
Description: This is a digitally signed message part
_______________________________________________ gdal-dev mailing list gdal-dev@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/gdal-dev