The Postgis Geometry data type, for example imported from OpenStreetMap maps

For my project needed to build the pedestrian routes and to consider their length.
I solved this problem by using pgrouting, which in turn relies on postgis.
Postgis is an extension to Postgresql that implements the standard OpenGis.
In this extension contains extensive functionality for working with spatial data.
This allows you to write interesting applications.
In particular, OpenStreetMap is using postgis to display your cards.
I will try to tell about how to look at postgis, imported osm maps.

I'll skip the description of how to install postgres and postgis.
Start with creating a database to store the spatial data.

Create database:

the
create database openstreetmap;

Initialize postgis:

the
create extension postgis;

We have in the database will appear in the table spatial_ref_sys, which is nearly 4 thousand records.
Each entry corresponds to a spatial coordinate system that defines the projection of longitude and latitude onto a plane.
Using this postgis table can convert data from one projection to another.

We will create a table in our spatial data:

the
create table my_map(
id bigserial primary key,
name text,
shape geometry
);

In this table, the shape field is of type geometry.
In postgis this is the basic data type to represent objects in a Euclidean coordinate system.

We will add the created table the point corresponding to the center of Saint-Petersburg:

the
insert into my_map(name, shape) values ('Tsentr SPb', ST_Point(30.3250575, 59.9174455));

The ST_Point function creates an object with a point geometry type.
Also have ST_MakeLine, ST_MakeEnvelope, ST_MakePolygon and other useful constructors.

Now you can proceed to download the osm maps.
For this we need myself map and osm2pgsql.
I'm not going to tell you how to install osm2pgsql.
It depends on the system and on windows seems non-trivial.
Download file russia-european-part-latest.osm.pbf run the import:

the
osm2pgsql -d openstreetmap -U iakov -C 2000 russia-european-part-latest.osm.pbf

I think only non-obvious parameter Since it determines the size, in megabytes, of the cache used when downloading.
By default, it is equal to 800 and for a specific file is not enough, so I increased it to 2000.
Immediately after the launch it appears:

the
NOTICE: table "planet_osm_point" does not exist, skipping

This is not a bug. Postgis tries to delete the necessary tables. So usage of them does not yet exist.

Considerable time is data processing:

the
Processing: Node(83577k 1816.9 k/s) Way(8382k 37.42 k/s) Relation(245290 939.81/s) parse time: 531s

It is processed by the osm object. Node are nodes that correspond to points on the map. Way is a path, an ordered list of nodes.
Relation — the relation that can be joined to other elements(nodes, ways, relations).
More about this can be read on openstreetmap wiki.

At the end of the database appeared 4 table: planet_osm_line, planet_osm_point, planet_osm_polygon, planet_osm_roads.
Stored in planet_osm_point processed nodes are stored in planet_osm_line an open path planet_osm_polygon stored in closed paths, planet_osm_roads paths are stored, which correspond to roads.
In each table there is a field of type geometry in the way.
You can see the geometry type using GeometryType:

the
openstreetmap=# select GeometryType(way) from planet_osm_point group by GeometryType(way);
geometrytype
--------------
POINT
(1 row)

openstreetmap=# select GeometryType(way) from planet_osm_line group by GeometryType(way);
geometrytype
--------------
LINESTRING
(1 row)


openstreetmap=# select GeometryType(way) from planet_osm_polygon group by GeometryType(way);
geometrytype
--------------
MULTIPOLYGON
POLYGON
(2 rows)

openstreetmap=# select GeometryType(way) from planet_osm_roads group by GeometryType(way);
geometrytype
--------------
LINESTRING
(1 row)

The four types POINT, LINESTRING, POLYGON, MULTIPOLYGON enough to store the map.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

March Habrameeting in Kiev

PostgreSQL load testing using JMeter, Yandex.Tank and Overload

Monitoring PostgreSQL with Zabbix