TransWikia.com

Generate or create OGC WMS using Python mapnik

Geographic Information Systems Asked on June 18, 2021

I want to create an OGC WMS server from mapnik PostGIS using Python.

I have successfully post table on PostGIS using:

shp2pgsql -s 4326 /Users/apple/Downloads/111SHAPEFILE/Point.shp points | psql -d gis -U apple -h localhost  

then I successfully connect mapnik with PostGIS database using:

from mapnik import PostGIS
postgis = PostGIS(dbname="gis", user = "apple", host = "localhost", port = '' ,password = "", table= 'points')

I want to create OGC WMS server from mapnik, how can I do this?

One Answer

Try Mapproxy that does what you want https://mapproxy.org e.g serve Mapnik based project(s) as WMS

There is also this old project but unmaintained https://github.com/mapnik/OGCServer

In both cases, you need to write your Mapnik configuration to XML and use this XML as an input. This XML can be generated via Python by declaring your datasource, styles, etc and finish by using save_map_to_string method from mapnik like below sample (it uses both shapefile and PostGIS as data sources)

import mapnik 

# Instantiate a map object with given width, height and spatial reference 
# system 
m = mapnik.Map(600,300,"+proj=latlong +datum=WGS84") 
# Set background colour to 'steelblue'. 
# You can use 'named' colours, #rrggbb, #rgb or rgb(r%,g%,b%) format 
m.background = mapnik.Color('steelblue') 
                                                                                                                                    

s = mapnik.Style() 
# A Style can have one or more rules. A rule consists of a filter, min/max 
# scale demoninators and 1..N Symbolizers. If you don't specify filter and 
# scale denominators you get default values : 
#   Filter =  'ALL' filter (meaning symbolizer(s) will be applied to all 
#   features) 
#   MinScaleDenominator = 0 
#   MaxScaleDenominator = INF 
# Lets keep things simple and use default value, but to create a map we 
# we still must provide at least one Symbolizer. Here we  want to fill 
# countries polygons with greyish colour and draw outlines with a bit 
# darker stroke. 
r=mapnik.Rule()
polySym = mapnik.PolygonSymbolizer() 
polySym.fill = mapnik.Color('#f2eff9') 
r.symbols.append(polySym) 
lineSymb = mapnik.LineSymbolizer() 
lineSymb.stroke = mapnik.Color('rgb(50%,50%,50%)') 
r.symbols.append(lineSymb) 
s.rules.append(r)

# Here we have to add our style to the Map, giving it a name.
m.append_style('My Style',s)

# Here we instantiate our data layer, first by giving it a name and srs
# (proj4 projections string), and then by giving it a datasource.
lyr = mapnik.Layer('world',"+proj=longlat +datum=WGS84 +no_defs")
# Then provide the full filesystem path to a shapefile in WGS84 or
# EPSG 4326 projection without the .shp extension
# A sample shapefile can be downloaded from
# https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip
lyr.datasource = mapnik.Shapefile(file='/home/thomasg/Téléchargements/ne_10m/ne_10m_admin_0_countries')

lyr1 = mapnik.Layer('populated_places',"+proj=longlat +datum=WGS84 +no_defs")
lyr1.datasource = mapnik.PostGIS(dbname="states",
    user="osm", host="localhost",
    password = "osm", table= 'ne_110m_populated_places')


s1 = mapnik.Style()
r1 = mapnik.Rule()
markerSym = mapnik.MarkersSymbolizer()
markerSym.fill = mapnik.Color("darkorange")
markerSym.stroke= mapnik.Color("orange")
r1.symbols.append(markerSym)
s1.rules.append(r1)
m.append_style('Style Points',s1)

lyr1.styles.append('Style Points')

lyr.styles.append('My Style')

m.layers.append(lyr)
m.layers.append(lyr1)

m.zoom_to_box(lyr1.envelope())

# Write the data to a png image called world.png in the base directory of your user to see infos
mapnik.render_to_file(m,'world.png', 'png')

# print(mapnik.save_map_to_string(m))

with open('my_mapnik.xml', 'w') as infile:
    infile.write(mapnik.save_map_to_string(m))
# Exit the python interpreter
exit()

You can also generated manually or using CartoCSS editor and generate your XML from the CartoCSS styles.

Answered by ThomasG77 on June 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP