0
1

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's gravatar image

Zoltan_Szecsei
11112
accept rate: 0%


3 Answers:
0

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)

permanent link

answered 22 Jul '15, 13:51

maxerickson's gravatar image

maxerickson
11.4k1075161
accept rate: 30%

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
0

I am using this:

m = mapnik.Map(image_width,image_height)
mapnik.load_map(m, v['style'])
bbox=mapnik.Envelope( left, top, right, bottom )
m.zoom_to_box(bbox)
mapnik.render_to_file(m, v['out'],'jpeg')

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.

permanent link

answered 22 Jul '15, 15:34

stf's gravatar image

stf
8.6k969137
accept rate: 18%

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
0

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.

#! /bin/bash
  # Define defaults
  state_file="california-latest.osm.pbf"
  state_server="http://download.geofabrik.de/north-america/us/"
  cache_dir="osm/"
  osm_style="${cache_dir}osm2pgsql.style"
  if [ -e "${cache_dir}${state_file}" ] ; then
    mv -f "${cache_dir}${state_file}" "${cache_dir}${state_file}.bak"
  fi
  wget -O "${cache_dir}${state_file}" "${state_server}${state_file}"
  echo "Filling PostGIS database"
  cmd="osm2pgsql --verbose --create --multi-geometry --database osm --style ${osm_style} --hstore ${cache_dir}${state_file}"
  echo ${cmd}
  ${cmd}
  echo 'CREATE INDEX idx_planet_osm_point_tags ON planet_osm_point USING gist(tags);' > pgsql osm
  echo 'CREATE INDEX idx_planet_osm_polygon_tags ON planet_osm_polygon USING gist(tags);' > pgsql osm
  echo 'CREATE INDEX idx_planet_osm_line_tags ON planet_osm_line USING gist(tags);' > pgsql osm

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:

    <Style name="buildings">
    <Rule>
        <Filter>[building] != ''</Filter>
            <PolygonSymbolizer fill="rgb(0,0,0)" />
    </Rule>
</Style>

<Style name="fences">
    <Rule>
        <Filter>
            ([barrier] != '')
        </Filter>
        <LineSymbolizer
            stroke="rgb(0,0,0)"
            stroke-width="&pt_size_0_5;"
            stroke-dasharray="&pt_size_0_5;,&pt_size_0_5;"
        />
    </Rule>
</Style>

<Layer name="buildings" status="on" srs="&osm2pgsql_projection;">
    <StyleName>buildings</StyleName>
    <Datasource>
        <Parameter name="type">postgis</Parameter>
        <Parameter name="host">localhost</Parameter>
        <Parameter name="dbname">osm</Parameter>
        <Parameter name="user"></Parameter>
        <Parameter name="password"></Parameter>
        <Parameter name="table">
            (SELECT way, building
            from planet_osm_polygon where building is not null) as foo
        </Parameter>
    </Datasource>
</Layer>

<Layer name="fences" status="on" srs="&osm2pgsql_projection;">
    <StyleName>fences</StyleName>
    <Datasource>
        <Parameter name="type">postgis</Parameter>
        <Parameter name="host">localhost</Parameter>
        <Parameter name="dbname">osm</Parameter>
        <Parameter name="user"></Parameter>
        <Parameter name="password"></Parameter>
        <Parameter name="table">
            (SELECT way, barrier
            from planet_osm_line where (barrier = 'fence') or (barrier = 'gate')) as foo
        </Parameter>
    </Datasource>
</Layer>
permanent link

answered 22 Jul '15, 16:04

stf's gravatar image

stf
8.6k969137
accept rate: 18%

edited 22 Jul '15, 16:09

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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×396
×322
×56
×53
×6

question asked: 22 Jul '15, 12:58

question was seen: 8,224 times

last updated: 22 Jul '15, 17:14

powered by OSQA