There are available GIS Data that can be used in your application.
In this lecture we are going to illustrate how to download data from Open Street Maps and to import these data in your application.
Let's explore how to do it.
Once you are logged in into Open Street Maps, you can download a set of data for you focus area by simply clicking the Export link.
The view area will be selected automatically. You can change the coordinates to change the boundaries of the area or manually select a different area.
It will be downloaded a file named map.osm by default, containing GIS data for the selected area.
The map.osm file is an xml bases file that contains data about the GIS objects in the area.
Below is a short sample from the file content
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="openstreetmap-cgimap 2.0.1 (2557171 spike-08.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<bounds minlat="41.3315320" minlon="19.8211280" maxlat="41.3338880" maxlon="19.8260740"/>
<node id="1842432778" visible="true" version="5" changeset="62307215" timestamp="2018-09-05T10:58:13Z" user="osmbushido" uid="7114752" lat="41.3318247" lon="19.8219132"/>
<node id="1842432782" visible="true" version="7" changeset="76672161" timestamp="2019-11-06T00:31:07Z" user="Araucaria_araucana" uid="9202927" lat="41.3331956" lon="19.8228713">
<tag k="name" v="Sheshi Selvia"/>
<tag k="place" v="square"/>
</node>
<node id="1842432783" visible="true" version="5" changeset="76672161" timestamp="2019-11-06T00:31:07Z" user="Araucaria_araucana" uid="9202927" lat="41.3355132" lon="19.8241532"/>
......
There are several tools that you can use to import OSM data to postGIS.
In this tutorial we will use osm2pgsql.
osm2pgsql is available for different platform, including Linux and Windows.
Download and install the correct version according to your operating system.
Download link:
https://osm2pgsql.org/doc/install.html
If you are using Windows a compressed file archive osm2pgsqlIt--{version}.zip will download.
Extract the .zip archive.
Open a command line windows and navigate to the extracted folder (...bin postfix) and locate an .exe file named osm2pgsql.exe
Now you are ready to import your downloaded map.osm file to postgree database.
Prepare the database
The import procedure will create a number of tables in the database containing gis objects organized in geometry types for points, lines etc.
For this example create a new database in postgre, call it osm_db.
In the new database you will need to create postgis extension and htype extensions.
In PGAdmin open a query editor connected to your osm_db database.
Create postgis extension
create extension postgis;
create hstore extension;
create extension hstore;
hstore is a key-value datatype in postrgree used to store open street maps. This allows you to store and query multiple OSM key-value pairs in a single column.
Make sure that you remember the username and password for accessing postgre.
Copy and paste the downloaded file map.osm in the folder where osm2pgsql.exe file is located.
Run the following command to import the data in the database
osm2pgsql -d osm_db -U postgres -W --create --slim -G -H localhost -S default.style --hstore map.osm
It will ask you to enter the password of postgre for user postgre. Enter your password and wait for the import process to finish
Below is a sample of the command output.
C:\gis\osm2pgsql\osm2pgsql-bin>osm2pgsql -d osmtest -U postgres -W --create --slim -G -H localhost -S default.style --hstore map.osm
2025-04-04 08:10:44 osm2pgsql version 1.10.0
Password:
2025-04-04 08:10:49 Database version: 17.0
2025-04-04 08:10:49 PostGIS version: 3.5
2025-04-04 08:10:49 Storing properties to table '"public"."osm2pgsql_properties"'.
2025-04-04 08:10:49 Setting up table 'planet_osm_point'
2025-04-04 08:10:49 Setting up table 'planet_osm_line'
2025-04-04 08:10:49 Setting up table 'planet_osm_polygon'
2025-04-04 08:10:49 Setting up table 'planet_osm_roads'
2025-04-04 08:10:49 Reading input files done in 0s.
2025-04-04 08:10:49 Processed 2429 nodes in 0s - 2k/s
2025-04-04 08:10:49 Processed 315 ways in 0s - 315/s
2025-04-04 08:10:49 Processed 14 relations in 0s - 14/s
2025-04-04 08:10:49 Clustering table 'planet_osm_point' by geometry...
2025-04-04 08:10:49 Clustering table 'planet_osm_line' by geometry...
2025-04-04 08:10:49 Clustering table 'planet_osm_roads' by geometry...
2025-04-04 08:10:49 Clustering table 'planet_osm_polygon' by geometry...
2025-04-04 08:10:49 Creating geometry index on table 'planet_osm_roads'...
2025-04-04 08:10:49 Creating geometry index on table 'planet_osm_polygon'...
2025-04-04 08:10:49 Creating geometry index on table 'planet_osm_point'...
2025-04-04 08:10:49 Creating geometry index on table 'planet_osm_line'...
2025-04-04 08:10:49 Creating osm_id index on table 'planet_osm_roads'...
2025-04-04 08:10:49 Creating osm_id index on table 'planet_osm_point'...
2025-04-04 08:10:49 Creating osm_id index on table 'planet_osm_polygon'...
2025-04-04 08:10:49 Creating osm_id index on table 'planet_osm_line'...
2025-04-04 08:10:49 Analyzing table 'planet_osm_roads'...
2025-04-04 08:10:49 Analyzing table 'planet_osm_polygon'...
2025-04-04 08:10:49 Analyzing table 'planet_osm_point'...
2025-04-04 08:10:49 Analyzing table 'planet_osm_line'...
2025-04-04 08:10:49 Done postprocessing on table 'planet_osm_nodes' in 0s
2025-04-04 08:10:49 Building index on table 'planet_osm_ways'
2025-04-04 08:10:49 Done postprocessing on table 'planet_osm_ways' in 0s
2025-04-04 08:10:49 Building index on table 'planet_osm_rels'
2025-04-04 08:10:49 Done postprocessing on table 'planet_osm_rels' in 0s
2025-04-04 08:10:49 All postprocessing on table 'planet_osm_point' done in 0s.
2025-04-04 08:10:49 All postprocessing on table 'planet_osm_line' done in 0s.
2025-04-04 08:10:49 All postprocessing on table 'planet_osm_polygon' done in 0s.
2025-04-04 08:10:49 All postprocessing on table 'planet_osm_roads' done in 0s.
2025-04-04 08:10:49 Storing properties to table '"public"."osm2pgsql_properties"'.
2025-04-04 08:10:50 osm2pgsql took 0s overall.
C:\gis\osm2pgsql\osm2pgsql-bin>
As you can see from the command output a number of tables are created
planet_osm_point
planet_osm_line
planet_osm_polygon
planet_osm_roads
Verify in PGAdmin that these tables are created
Stores point features (e.g., restaurants, shops, bus stops, ATMs, traffic lights).
Each row represents a single point.
Important columns:
osm_id – OSM ID of the point.
tags (if --hstore is used) – Stores OSM key-value tags.
name – Name of the feature (if available).
amenity, shop, tourism – Extracted attributes (if defined in the .style file).
way – The geometry column (stores the point location).
Example query to get all restaurants:
SELECT name, way
FROM planet_osm_point
WHERE amenity = 'restaurant';
planet_osm_point table stores objects.
Let us verify what object types do we have in the downloaded data if Central Italy
select amenity,count(*)
from planet_osm_point
group by amenity
Stores line features (e.g., roads, rivers, railways, power lines).
Each row represents a line (polyline) feature.
Important columns:
osm_id – OSM ID of the line.
tags – Stores OSM key-value pairs.
highway, railway, waterway – Type of linear feature.
way – Geometry column (stores the line geometry).
Example query to get all major highways:
SELECT name, highway, way
FROM planet_osm_line
WHERE highway IN ('motorway', 'primary', 'secondary');
Stores polygon features (e.g., buildings, parks, lakes, forests).
Each row represents a closed area.
Important columns:
osm_id – OSM ID of the polygon.
tags – Stores OSM key-value pairs.
building, landuse, leisure, natural – Extracted attributes.
way – Geometry column (stores the polygon geometry).
Example query to get all parks:
SELECT name, way
FROM planet_osm_polygon
WHERE leisure = 'park';
Stores a simplified version of roads (subset of planet_osm_line).
Used mainly for rendering in cartographic applications.
Similar to planet_osm_line, but contains only main roads.
The planet_osm_node table contains all point-type features from OpenStreetMap (OSM) data. In OSM, nodes are the most basic element: they represent specific points in space, and they can be used to mark things like:
Points of interest (POIs) (e.g., restaurants, gas stations, landmarks)
Infrastructure points (e.g., traffic signals, gates, or junctions)
Geographic points (e.g., mountain peaks, city centers)
In short, nodes represent locations in space that don't necessarily form lines or areas, but instead, mark specific spots.
You can download more data from
https://download.geofabrik.de/
Example, let's download and import data of Italy.
Last note,
remember SRID transformation
select
--ST_SRID(ST_AsText(ST_Transform(ST_SetSRID(geometry, 4326), 3857))) new_srid,
--ST_AsText(ST_Transform(ST_SetSRID(geometry, 4326), 3857)) transformedCoordinates ,
ST_SRID(geometry),
ST_ASTEXT(geometry)
from municipalities2