Using OpenStreetMap (OSM) data to build a map service

Source: Internet
Author: User
Tags polyline psql postgis

Http://www.cnblogs.com/LBSer/p/4451471.html

Map of Beijing using the OSM data for simple publishing

First, what is OSM?

Open Street Maps (OPENSTREETMAP, OSM) is an online map collaboration program that aims to create a world map of content that is free and editable for everyone (wiki:http://wiki.openstreetmap.org/wiki/ Main_Page). In particular, the OSM data is open source and can be freely downloaded and used.

II. OSM Data structure

OpenStreetMap includes spatial data and attribute data. The spatial data mainly includes three kinds: Point (Nodes), road (Ways) and relation (Relations), these three kinds of primitive constitute the whole map picture. WhereNodes defines the position of the midpoint of the space,Ways defines the line or region, and Relations(optional) defines the relationship between the elements.

Attribute data tags are used to describe the above vector data primitives. (http://wiki.openstreetmap.org/wiki/Elements)

2.1. Node

Node defines a geographic coordinate point by latitude. At the same time, height=* can be used to mark the elevation of objects, through layer=* and level =*, You can indicate the number of layers in the map plane and the building where the object resides, andthe name of the object by place =* and name=*. at the same time, the means is made up of multiple points (node) connected to a line (polygon).

2.2.

The means is made up of 2-2000 points (nodes) . the way can represent the following 3 graphical objects (non-closed line (Open polyline ), closed line (Closed polyline), region (area). For a means that exceeds nodes, it can be processed by splitting.

a) Open polyline

Non-closed line: Closes an unclosed segment. It can often be used to represent real-world roads, rivers, railways, and so on.

b) Closed polyline

Closing line: Closes the connected line. For example, you can express the reality of the circular Line subway.

c) area

Area: Closed area. landuse=* are usually used to mark areas.

2.3. Relation

A Relation is used to describe the interrelationships of two or more primitives (nodes, ways, or other relations), and the relationships are defined by role , including:

A) route: Define roads, bike lanes, railways, etc.

b) Multiple polygons: Defining area such as buildings, embankments, etc.

c) Boundary: door-mounted to define administrative boundaries

d) Restrictions: used to describe restrictions such as "non-left"

2.4. Tag

Tags are not the basic elements of a map, but each element uses tag to record data information. Record the data with ' key ' and a ' value ' (Should you understand the XML or database?) )。 For example, you candefine a residential road by highway =Residential , and you can use additional namespaces to add additional information, such as:maxspeed:winter the =* represents the highest speed limit in winter.

Third, OSM data and download

The OSM data format mainly has the following, can download data through the following website.

OSM Data format:

  • OSM XML –xml-format provided by the API
  • PBF –highly Compressed, optimized binary format similar to the API
  • o5m –for High-speed processing, uses PBF coding, has same structure as XML format
  • OSM JSON –json variant of OSM XML

OSM Data Download Website:

Geofabrik: http://www.geofabrik.de/

Metro Extracts: http://metro.teczno.com/

Hot exports: http://hot.openstreetmap.org/

Bbbike: http://extract.bbbike.org/

Iv. setting up a map service based on OSM data

The OSM wiki provides an architectural diagram, which is valuable, and we can explore it in a single step as per the architecture diagram.

4.1. Database

The postgresql+ plug-in PostGIS is ideal for storing geospatial data, as is the underlying database for the architecture diagram above, so we also use this pairing.

A) Download Postgresql+postgis plugin

sudo apt-get install PostgreSQL postgresql-contrib postgis postgresql-9.1-postgis

After installation, we need to change the password of the Postgres user, otherwise we will not be able to use this database server. To run the Psql command as a user of the Postgres system, enter the following in the terminal:

sudo su postgres-c psql template1

At this point, a new prompt will appear, enter the following two commands, replace <***password***> with a new password;:

ALTER USER postgres with PASSWORD ' <***password***> ';

b) Create users and databases

Postgres# CREATE USER zhanlijun with PASSWORD ' xxxx ';

Postgres# CREATE DATABASE OSM;

Postgres# GRANT all privileges on the DATABASE osm to Zhanlijun;

c) Add a space extension to the database

CREATE EXTENSION PostGIS;

--Enable topology

CREATE EXTENSION postgis_topology;

--fuzzy matching needed for Tiger

CREATE EXTENSION Fuzzystrmatch;

--geo-coding

CREATE EXTENSION Postgis_tiger_geocoder;

--For storing properties Tags,key-value

CREATE EXTENSION Hstore;

4.2. Import data

The OSM data downloaded from the website is generally file format (such as XML, PBF, etc.), in order to use we need to import it into the database, this requires the import tool, the frame composition uses the osmpsis, but the use is not friendly, recommend the use of Osm2pgsql.

A) Install Osm2pgsql

  1. Run sudo apt-get install Software-properties-common to Install the command add-apt-repository if the command can ' t be found.
  2. Run sudo add-apt-repository ppa:kakrueger/openstreetmap to add the PPA
  3. Run sudo apt-get update to update your packaging system.
  4. Run sudo apt-get install osm2pgsql to install the Osm2pgsql package.

b) Install Protobuf (in order to import PBF format data)

sudo apt-get install libprotobuf-c0-dev protobuf-c-compiler

c) Import the database

Osm2pgsql-s-u zhanlijun-d osm/users/zhanlijun/downloads/planet_116.104,39.667_116.892,40.066.osm.pbf-h localhost- W

Note:There are two modes of osm2pgsql importing data, Normal and slim mode.

Normal mode produces the following three intermediate tables in memory and discards them at the end of the import, so it is faster.

    • Planet_osm_nodes
    • Planet_osm_ways
    • Planet_osm_rels

The slim mode, in turn, places the intermediate results completely in the database. The benefits of Slim mode are easy to update.

The difference between the two uses is whether to add "-S", plus the expression Slimmode, this article uses slim mode.

After importing data using Slim mode, the following table is generated in the database.

4.3. Rendering

The data has been imported into PostgreSQL, and the data below needs to be rendered, that is, the vector data in PostgreSQL is rendered as a picture.

6.1. Using Mapnik for rendering

The most famous open source map rendering engine is Mapnik.

Mapnik can render a variety of data sources, including databases such as PostgreSQL, and file format data such as Shapefiles, osm.xml formats, and more.

Here, for example, shapefiles format data, download the vector data that needs to be rendered:http://www.naturalearthdata.com/

1) Open the Python editor

Python

2) import Mapnik python bindings

Import Mapnik

3) Create a map

M = mapnik. Map (h) # Create a map: Wide

#m. SRS is the projection of the map, default is ' +proj=longlat +ellps=wgs84 +datum=wgs84 +no_defs '

M. Background = Mapnik. Color (' Steelblue ') # Set background color

4) Create a style

Styling is based on our needs, and the style determines the result of the final rendering.

s = mapnik. Style () #style object

R = Mapnik. Rule () #rule object to manage symbols

# Polygon Fill symbol

Polygon_symbolizer = mapnik. Polygonsymbolizer (Mapnik. Color (' #f2eff9 '))

R. Symbols. Append (Polygon_symbolizer)

# polygon Boundary fill symbol

Line_symbolizer = mapnik. Linesymbolizer (Mapnik. Color (' rgb (50%,50%,50%) '),0.1)

R. Symbols. Append (Line_symbolizer)

S. Rules. Append (R)

# Add styles to a map

M. Append_style (' My style ', s)

5) Create a data source

Ds=mapnik. Shapefiles (file= ' users/zhanlijun/downloads/110m-admin-0-countries/ne_110m_admin_0_countries.shp ')

6) Create a layer

The layer of the mapnik is the underlying container for the data

Layer = Mapnik. Layer (' World ') # Create a new layers called World

#layer. SRS Default is ' +proj=longlat +ellps=wgs84 +datum=wgs84 +no_defs '

Layer. DataSource = ds

Layer. Styles. Append (' My Style ')

7) Prepare map rendering

M. Layers. Append (layer) # adds a layer to the map

M. Zoom_all () # Zoom_all The data, if not, the result will be blank

8) Render Map

Finally we get a map of the world in PNG format: world.png

6.2. Using Tilemill for rendering

Mapnik is not easy to use, especially when configuring the style, below we use Tilemill to render, Tilemill kernel is mapnik.

The benefit of Tilemill is that what you see is what you get, the right configuration style, and the left side to show the results immediately. In addition, the results can be displayed.

7. Integrated Solutions

GeoServer + openlayers +postgis overlay to display dynamic vector data.

Installing GeoServer

sudo apt-get update

sudo apt-get install unzip OPENJDK-6-JRE

echo "Export Java_home=/usr/lib/jvm/java-6-openjdk-amd64" >> ~/.BASHRC

SOURCE ~/.BASHRC

Wget-c Http://sourceforge.net/projects/geoserver/files/GeoServer/2.3.5/geoserver-2.3.5-bin.zip

Unzip-a Geoserver-2.3.5-bin.zip

CD Geoserver-2.3.5/bin

./startup.sh &

Visit: http://localhost:8080/geoserver/

Using OpenStreetMap (OSM) data to build a map service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.