Hi, I'm struggling to render osm data in python, using mapnik. I have downloaded south-africa-and-lesotho-latest.osm.pbf, used imposm3 with provided mapping.json file, to load into postgis. For simple rendering, the following code snippet works - but I would like to render my maps using osm styles, rather than simple lines, etc. How would I tell mapnik to use a specific rendering style, and where can I get rendering style from? I have looked at gravitystorm and standard_tile_layer, but can't work out how to implement them in python (below). Thanks in advance, Zoltan #!/usr/bin/env python import mapnik m = mapnik.Map(7015,4961) # / A4 LAndscape 600dpi / m.background = mapnik.Color('blue') m.background = mapnik.Color('white') s = mapnik.Style() r = mapnik.Rule() #polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('rgb(0,255,255)')) #r.symbols.append(polygon_symbolizer) line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('rgb(255,0,0)'),5) r.symbols.append(line_symbolizer) s.rules.append(r) m.append_style('My Style',s) lyr = mapnik.Layer('Geometry from PostGIS') lyr.datasource = mapnik.PostGIS(host='192.168.0.91',user='osmgis',password='xxxx',dbname='gisdata',table='osm.osm_roads',srid=3857) lyr.styles.append('My Style') m.layers.append(lyr) extent = mapnik.Box2d(2052835,-4039814,2056207,-4036860) m.zoom_to_box(extent) mapnik.render_to_file(m,'rsa_osm.png', 'png') asked 22 Jul '15, 12:58 Zoltan_Szecsei |
For the Python/mapnik part of the question take a look at nik4: https://github.com/Zverik/Nik4 (or maybe one of the other tools mentioned in the readme there) answered 22 Jul '15, 13:51 maxerickson Cool. Will take a look at nik4 now. Thanks & regards, Zoltan
(22 Jul '15, 13:56)
Zoltan_Szecsei
OK, thanks, but I can't see how nik4 will help, as I already have my data in postgis, and not in XML format. (am I getting muddled between osm data exported as XML, and styling XML files? To rephrase my problem: I have OSM data, from PBF format, loaded into PostGIS (used imposm3 to get it into PostGIS) I wish to (via a batch script/program) zoom into an arbitrary area, and render that area into a PNG file. I don't want tiling or anything else. Just simply a rendered image, in osm style. I've tried doing it with mapnik & python (see script snippet in my forst post), but is my approach wrong? This must surely be the most standard thing anyone does with (downloaded) osm data. TIA< Zoltan
(22 Jul '15, 14:58)
Zoltan_Szecsei
Yes, I think you are getting the XML types muddled. nik4.py itself doesn't care where the data is stored, it does the chore of passing the style XML and desired bounds/zoom into mapnik (so for example, the XML style file might have a connection to a PostGIS table). In essence, I think you are probably working on building something very similar to what nik4 does.
(22 Jul '15, 16:21)
maxerickson
|
I am using this:
Where v['style'] points to a mapnik XML style sheet. This is the older way to style mapnik, the newer being to use a CSS file that, I think, is used to generate mapnik XML. Some information to get you started is here: https://github.com/mapnik/mapnik/wiki/XMLConfigReference The mapnik XML is not as well documented as I'd like and you need to be sure that the tags your style(s) need are actually inserted into the Postgresql database. answered 22 Jul '15, 15:34 stf Hi, OSM data can be downloaded as XML or PBF. In your v'style' example, that style sheet also has the map geometry in it, does it not? If not, what do you use to load your geometry? Thanks, Zoltan
(22 Jul '15, 15:47)
Zoltan_Szecsei
edit: I can't get the code to format properly, so posting new answer to your comment. No, the mapnik style has no geometry in it. For each type of thing to be rendered there are two parts, one specifics how to get the data (from database with query, from XML, from other file, etc.) and one that specifies how it is to be rendered.
(22 Jul '15, 16:02)
stf
|
Here is the script I use to get OSM data and to load it into my database. The "style" file for osm2pgsql is actually in a column format listing the tagging on the objects that are to be loaded.
For rendering, the mapnik XML has one part that lists what and how to render another part that gets the data. Here is how I render buildings and fences:
answered 22 Jul '15, 16:04 stf Ugh! Why does this site lose the \n????? I'm deciphering above now......
(22 Jul '15, 16:09)
Zoltan_Szecsei
So your mapnik XML file (above file with <style name="buildings"> etc) is called style, and that;s how you point to it with your rendering script, using: m = mapnik.Map(image_width,image_height) mapnik.load_map(m, v['style']) ??
(22 Jul '15, 16:38)
Zoltan_Szecsei
Basically yes. In actuality the file called out by v['style'] is a XML file that defines some entities and then includes files like the one I posted above. That way I can do different maps with different things displayed pretty easily.
(22 Jul '15, 16:46)
stf
And that is exactly the (default) style file I am looking to download and use. Is it available somewhere?
(22 Jul '15, 16:51)
Zoltan_Szecsei
Actually, no: All the style sheets I have I created myself and my organization is unique to me. The default rendering used to be directly by mapnik xml but is now through CartoCSS. The current carto CSS stuff is at https://github.com/gravitystorm/openstreetmap-carto And a link there to the old stuff points to https://trac.openstreetmap.org/browser/subversion/applications/rendering/mapnik#inc But I haven't worked my way through that to verify everything is there. Looks like a lot of scripts and Python stuff are there in addition to style information. If you want to use the current techniques then you probably want to be working off of the cartoCSS stuff rather than directly writing the mapnik XML. I got started before cartoCSS was released and haven't bothered to update as what I have works for me.
(22 Jul '15, 17:05)
stf
Ugh. OK - will take a longer look at building something with cartoCSS. If I have to learn, might as well start with 'current' than 'old'. Big thanks for your input. Regards, Zoltan
(22 Jul '15, 17:14)
Zoltan_Szecsei
showing 5 of 6
show 1 more comments
|