def getMapForm(shapefile): """ Return a form.Form subclass for editing the given shapefile's features. The form will have a single field, 'geometry', which lets the user edit the feature's geometry. """ # Setup a dummy admin instance to auto-generate our map widget. geometryField = utils.calcGeometryField(shapefile.geom_type) geometryType = utils.calcGeometryFieldType(shapefile.geom_type) if geometryType == "Point": adminType = PointAdmin elif geometryType == "LineString": adminType = LineStringAdmin elif geometryType == "Polygon": adminType = PolygonAdmin elif geometryType == "MultiPoint": adminType = MultiPointAdmin elif geometryType == "MultiLineString": adminType = MultiLineStringAdmin elif geometryType == "MultiPolygon": adminType = MultiPolygonAdmin elif geometryType == "GeometryCollection": adminType = GeometryCollectionAdmin adminInstance = adminType(Feature, admin.site) field = Feature._meta.get_field(geometryField) widgetType = adminInstance.get_map_widget(field) # Define a form which encapsulates the desired editing field. class MapForm(forms.Form): geometry = forms.CharField(widget=widgetType(), label="") return MapForm
def tile(request, version, shapefile_id, zoom, x, y): """ Return a single Tile resource for our Tile Map Server. This returns the rendered map tile for a given zoom level, x and y coordinate. """ try: # Parse the supplied parameters to see which area of the map to # generate. if version != "1.0": raise Http404 shapefile = Shapefile.objects.get(id=shapefile_id) if shapefile == None: raise Http404 geometryField = utils.calcGeometryField(shapefile.geom_type) geometryType = utils.calcGeometryFieldType(shapefile.geom_type) zoom = int(zoom) x = int(x) y = int(y) if zoom < 0 or zoom > MAX_ZOOM_LEVEL: raise Http404 xExtent = _unitsPerPixel(zoom) * TILE_WIDTH yExtent = _unitsPerPixel(zoom) * TILE_HEIGHT minLong = x * xExtent - 180.0 minLat = y * yExtent - 90.0 maxLong = minLong + xExtent maxLat = minLat + yExtent if (minLong < -180 or maxLong > 180 or minLat < -90 or maxLat > 90): print "Map extent out of bounds:",minLong,minLat,maxLong,maxLat raise Http404 # Prepare to display the map. map = mapnik.Map(TILE_WIDTH, TILE_HEIGHT, "+proj=longlat +datum=WGS84") map.background = mapnik.Color("#7391ad") dbSettings = settings.DATABASES['default'] # Setup our base layer, which displays the base map. datasource = mapnik.PostGIS(user=dbSettings['USER'], password=dbSettings['PASSWORD'], dbname=dbSettings['NAME'], table='"shapeEditor_basemap"', srid=4326, geometry_field="geometry", geometry_table='"shapeEditor_basemap"') baseLayer = mapnik.Layer("baseLayer") baseLayer.datasource = datasource baseLayer.styles.append("baseLayerStyle") rule = mapnik.Rule() rule.symbols.append( mapnik.PolygonSymbolizer(mapnik.Color("#b5d19c"))) rule.symbols.append( mapnik.LineSymbolizer(mapnik.Color("#404040"), 0.2)) style = mapnik.Style() style.rules.append(rule) map.append_style("baseLayerStyle", style) map.layers.append(baseLayer) # Setup our feature layer, which displays the features from the # shapefile. query = '(select ' + geometryField + ' from "shapeEditor_feature" where shapefile_id=' + str(shapefile.id) + ') as geom' print "QUERY: " + query +":" + geometryField+":" + geometryType datasource = mapnik.PostGIS(user=dbSettings['USER'], password=dbSettings['PASSWORD'], dbname=dbSettings['NAME'], table=query, srid=4326, geometry_field=geometryField, geometry_table='"shapeEditor_feature"') print "DATA SOURCE IS " + str(datasource) featureLayer = mapnik.Layer("featureLayer") featureLayer.datasource = datasource featureLayer.styles.append("featureLayerStyle") rule = mapnik.Rule() if geometryType in ["Point", "MultiPoint"]: rule.symbols.append(mapnik.PointSymbolizer()) elif geometryType in ["LineString", "MultiLineString"]: rule.symbols.append( mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5)) elif geometryType in ["Polygon", "MultiPolygon"]: rule.symbols.append( mapnik.PolygonSymbolizer(mapnik.Color("#f7edee"))) rule.symbols.append( mapnik.LineSymbolizer(mapnik.Color("#000000"), 0.5)) style = mapnik.Style() style.rules.append(rule) map.append_style("featureLayerStyle", style) map.layers.append(featureLayer) # Finally, render the map. map.zoom_to_box(mapnik.Envelope(minLong, minLat, maxLong, maxLat)) image = mapnik.Image(TILE_WIDTH, TILE_HEIGHT) mapnik.render(map, image) imageData = image.tostring('png') return HttpResponse(imageData, mimetype="image/png") except: traceback.print_exc() raise