Created
July 26, 2010 21:47
-
-
Save dwins/491308 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from django.contrib.gis.geos import GEOSGeometry | |
| import json | |
| import sys | |
| bluemarble = r"""{ | |
| "fields": { | |
| "fixed": true, | |
| "format": null, | |
| "group": "background", | |
| "layer_params": "{\"args\": [\"bluemarble\", \"http://maps.opengeo.org/geowebcache/service/wms\", {\"layers\": [\"bluemarble\"], \"tiled\": true, \"tilesOrigin\": [-20037508.34, -20037508.34], \"format\": \"image/png\"}, {\"buffer\": 0}], \"type\": \"OpenLayers.Layer.WMS\"}", | |
| "map": %(mapid)d, | |
| "name": null, | |
| "opacity": 1.0, | |
| "ows_url": null, | |
| "source_params": "{\"ptype\": \"gx_olsource\"}", | |
| "stack_order": 0, | |
| "styles": null, | |
| "transparent": false, | |
| "visibility": true | |
| }, | |
| "model": "maps.maplayer", | |
| "pk": %(pk)d | |
| }""" | |
| ca = r"""{ | |
| "fields": { | |
| "fixed": true, | |
| "format": null, | |
| "group": "background", | |
| "layer_params": "{\"args\": [\"base:CA\", \"http://localhost:8001/geoserver/wms\", {\"layers\": [\"base:CA\"], \"tiled\": true, \"tilesOrigin\": [-20037508.34, -20037508.34], \"format\": \"image/png\"}, {\"buffer\": 0}], \"type\": \"OpenLayers.Layer.WMS\"}", | |
| "map": %(mapid)d, | |
| "name": null, | |
| "opacity": 1.0, | |
| "ows_url": null, | |
| "source_params": "{\"ptype\": \"gx_olsource\"}", | |
| "stack_order": 1, | |
| "styles": null, | |
| "transparent": false, | |
| "visibility": false | |
| }, | |
| "model": "maps.maplayer", | |
| "pk": %(pk)d | |
| }""" | |
| satellite = r"""{ | |
| "fields": { | |
| "fixed": true, | |
| "format": null, | |
| "group": "background", | |
| "layer_params": "{}", | |
| "map": %(mapid)d, | |
| "name": "SATELLITE", | |
| "opacity": 1.0, | |
| "ows_url": null, | |
| "source_params": "{\"apiKey\": \"ABQIAAAAkofooZxTfcCv9Wi3zzGTVxTnme5EwnLVtEDGnh-lFVzRJhbdQhQgAhB1eT_2muZtc0dl-ZSWrtzmrw\", \"ptype\": \"gx_googlesource\"}", | |
| "stack_order": 2, | |
| "styles": null, | |
| "transparent": false, | |
| "visibility": false | |
| }, | |
| "model": "maps.maplayer", | |
| "pk": %(pk)d | |
| } | |
| """ | |
| blank = r"""{ | |
| "fields": { | |
| "fixed": true, | |
| "format": null, | |
| "group": "background", | |
| "layer_params": "{\"args\": [\"No background\"], \"type\": \"OpenLayers.Layer\"}", | |
| "map": %(mapid)d, | |
| "name": null, | |
| "opacity": 1.0, | |
| "ows_url": null, | |
| "source_params": "{\"ptype\": \"gx_olsource\"}", | |
| "stack_order": 3, | |
| "styles": null, | |
| "transparent": false, | |
| "visibility": false | |
| }, | |
| "model": "maps.maplayer", | |
| "pk": %(pk)d | |
| }""" | |
| def layer_formatter(tpl, map): | |
| return lambda pk: json.loads(tpl % {"mapid": map["pk"], "pk": pk}) | |
| def extra_layers(map): | |
| formatters = [] | |
| for tpl in [bluemarble, ca, satellite, blank]: | |
| formatters.append(layer_formatter(tpl, map)) | |
| return formatters | |
| def fixup_map(map): | |
| fields = map["fields"] | |
| if "center_lat" in fields and "center_lon" in fields: | |
| xy = GEOSGeometry( | |
| "POINT(%f %f)" % (fields["center_lon"], fields["center_lat"]), | |
| srid=4326 | |
| ) | |
| xy.transform(3785) | |
| for k in ["center_lat", "center_lon", "endorsed", "featured"]: | |
| if k in fields: del fields[k] | |
| fields["center_x"] = xy.x | |
| fields["center_y"] = xy.y | |
| fields["projection"] = "EPSG:900913" | |
| def fixup_map_layer(layer): | |
| fields = layer["fields"] | |
| defaults = [ | |
| ("source_params", "{}"), | |
| ("layer_params", "{}"), | |
| ("fixed", False), | |
| ("visibility", True) | |
| ] | |
| for k, v in defaults: | |
| if k not in fields: fields[k] = v | |
| if __name__ == "__main__": | |
| dump = json.load(open(sys.argv[1])) | |
| base_layer_builders = [] | |
| max_used_pk = 0 | |
| for obj in dump: | |
| if obj["model"] == "maps.map": | |
| fixup_map(obj) | |
| base_layer_builders.extend(extra_layers(obj)) | |
| elif obj["model"] == "maps.maplayer": | |
| max_used_pk = max(max_used_pk, obj["pk"]) | |
| fixup_map_layer(obj) | |
| dump = dump + [builder(max_used_pk + 1 + i) for i, builder in enumerate(base_layer_builders)] | |
| output = sys.argv[1][:-5] if sys.argv[1][-5:] == ".json" else sys.argv[1] | |
| output = output + ".cleaned.json" | |
| json.dump(dump, open(output, "w")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment