A few weeks ago an interesting
PR for osm-carto
landed in the project's GitHub page. It adds rendering for several natural relief
features, adding ridges, valleys, aretes, dales, coulouirs and others to cliffs,
peaks and mountain passes, which were already being rendered. I decided to try
it in Elevation (offline for
the moment).
I sync'ed the style first with
the latest release,
applied the patch and... not much. My current database is quite old (re-importing
takes ages and I don't have space for updates), so I don't have much features
like that in the region I'm interested in. In fact, I went checking and
the closest mountain range around here was not in the database, so I added
it.
By the way, the range is mostly concurrent with a part of an administrative
boundary, but SomeoneElse
and
SK53
suggested to make a new line.
Even when other features are nearby (there's a path close to the crest and it's
also more or less the limit between a forest and a bare rock section), which already
makes the region a little bit crowded with lines, it makes sense: boundaries,
paths, forest borders and ridges change at different time scales, so having them
as separate lines makes an update to any of those independent of the rest.
Now I wanted to export this feature
and import it in my rendering database, so I can actually see the new part of the
style. This is not an straightforward process, only because when I imported my data I used
osm2pgsql --drop
, which removes the much needed intermediate tables for when
one wants to update with osm2pgsql --append
. Here's a roundabout way to go.
First you download the full feature (thanks RichardF
!). In this case:
http://www.openstreetmap.org/api/0.6/way/430573542/full
This not only exports the line (which is a sequence of references to nodes) with
its tags, but the nodes too (which are the ones storing the coords). The next
step is to convert it to something more malleable, for instance, GeoJSON. For
that I used ogr2ogr
like this:
ogr2ogr -f GeoJSON 430573542.GeoJSON 430573542.xml lines
The last parameter is needed because, quoting Even Rouault (a.k.a. José GDAL):
«you will always get "points", "lines", "multilinestrings", "multipolygons" and
"other_relations" layers when reading a osm file, even if some are empty», and
the GeoJSON driver refuses to create layers for you:
ERROR 1: Layer lines not found, and CreateLayer not supported by driver.
But guess what, that not the easiest way :) At least we learned something. In
fact postgis
already has a tool called shp2pgsql
that imports ESRIShapeFiles,
and ogr2ogr
produces by default this kind of file. It creates a .shp
file
for each layer as discussed before, but again, we're only interested in the line
one. So:
ogr2ogr 430573542 430573542.xml lines
shp2pgsql -a -s 900913 -S 430573542/lines.shp > 430573542.sql
We can't use this SQL file directly, as it has a couple of problems. First, you
can't tell shp2pgsql
the names of the table where you want to insert the data
or the geometry column. Second, it only recognizes some attributes (see below),
and the rest it tries to add them as hstore tags. So we have to manually edit
the file to go from:
INSERT INTO "lines" ("osm_id","name","highway","waterway","aerialway","barrier","man_made","z_order","other_tags",geom)
VALUES ('430573542','Montagne Sainte-Victoire',NULL,NULL,NULL,NULL,NULL,'0','"natural"=>"ridge"','010500002031BF0D[...]');
into:
INSERT INTO "planet_osm_line" ("osm_id","name","z_order","natural",way)
VALUES ('430573542','Montagne Sainte-Victoire','0','ridge','010500002031BF0D[...]');
See? s/lines/planet_osm_line/
, s/other_tags/"natural"/
(with double quotes,
because natural
is a keyword in SQL, as in natural join
), s/geom/way/
and
s/'"natural"=>"ridge"'/'ridge'/
(in single quotes, so it's a string; double
quotes are for columns). And I also removed the superfluous values and the
ANALIZE
line, as I don't care that much. Easy peasy.
A comment on the options for shp2pgsql
. -s 900913
declares the SRID of the
database. I got that when I tried without and:
ERROR: Geometry SRID (0) does not match column SRID (900913)
-S
is needed because shp2pgsql
by default generated MultiLineStrings, but
that table in particular has a LineString way
column. This is how I figure it
out:
ERROR: Geometry type (MultiLineString) does not match column type (LineString)
Incredibly, after this data massacre, it loads in the db:
$ psql gis < 430573542.sql
SET
SET
BEGIN
INSERT 0 1
COMMIT
Enjoy!