Tag Archives: GeoJSON

Use Overpass API to get OSM data

Overpass API

Have you ever needed very specific data from openstreetmap and don’t want to download the whole state or country? The solution is Overpass API.

The Overpass API (formerly known as OSM Server Side Scripting, or OSM3S) is a read-only API that serves up custom selected parts of the OSM map data. It acts as a database over the web: the client sends a query to the API and gets back the data set that corresponds to the query.

http://wiki.openstreetmap.org/wiki/Overpass_API

We are going to try the Overpass QL as query language. The whole query source code is divided in statements. We set the type of element that we want: node, way, relation or area. and then filter by tag. Like this

node[name="Foo"];
way[name="Foo"];
rel[name="Foo"];
area[name="Foo"];

Continue reading

Create a FeatureCollection object from a PostGIS query

Usually to render spatial data on a web map you need GeoJSON data  of the type “FeatureCollection”, and if you have your data on a  PostGIS database you can do a query to get  the geometries on GeoJSON format with the ST_AsGeoJSON(geom) function. Something like this:

SELECT loc_id, loc_name, ST_AsGeoJSON(geom, 4) as geojson FROM locations;

This will return a different row for every feature that should be on the collection, so after run that query you need a function to iterate over every result to insert loc_id and loc_name as properties of every feature and push them to the array of features.

But there is a more efficient way of doing it with this single trick query:

SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
   FROM (SELECT 'Feature' As type
    , ST_AsGeoJSON(lg.geom, 4)::json As geometry
    , row_to_json((SELECT l FROM (SELECT loc_id, loc_name) As l
      )) As properties
   FROM locations As lg ) As f ) As fc;

Continue reading

Georeferencing a scanned map with QGIS

Some times you need to have vector data that haven’t been digitized yet, like some old maps with building footprints or street lines. We can scan a map, georeference it in QGIS, and then digitize those features to create new vector layers. Scanned maps don’t contain spatial reference information, so to digitize we need to reference the raster in a specific coordinate system.

In this example we will georeference a map of the city of Barcelona with the layout of the old medieval walls (XIII-XIV) with QGIS.

Continue reading

Getting vector data from OpenStreetMap

Let’s say that we need the Barcelona’s subway lines in geojson for our project, and we decided to use OpenStreetMap data. There is plenty of ways for getting OSM data, the most common is using their API, or the  geofabrik download page,  but this time we gonna use metro-extracts from mapzen.

We look for Barcelona. There is a lot of file formats there:

metro-extracts-barcelona

metro-extracts-barcelona

Continue reading

Turf: GIS en la web

Los chicos de mapbox nos han traído un regalito muy especial este fin de año, una nueva librería de javascript llamada Turf.  Turf permite hacer operaciones geospaciales  (geoprocesamiento) directamente desde el navegador (#inabrowserbaby).

Turf es ligero, rápido y opensource (chupate esa ArcgisOnline)


Continue reading

Visualizar edificios 3D con OSM Buildings

OSM Buildings en una pequeña librería de Javascript que permite extruir las geometrías 2D de OpenStreetMap en 3D y visualizarlas en mapas interactivos. OSM Buildings no genera un verdadero mapa en 3D como sería el caso de Three.js, lo que hace es generar una perspectiva 3D en un mapa 2D.

http://osmbuildings.org/

OSMbuildings funciona tanto para OpenLayers como para Leaflet.js. En este ejemplo usaremos la librería de Leaflet para crear un mapa al que añadiremos los edificios en 3D como capa extra.

Continue reading

GeoJSON, ¿el formato del futuro?

En 2008 se define la especificación de un nuevo formato para datos geográficos basado en JSON (JavaScript Object Notation) llamado GeoJSON. Un objeto GeoJSON puede representar una geometría, features, o una colección de features y soporta geometrías de tipo: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, y GeometryCollection.

Las features en GeoJSON contienen una geometría y propiedades adicionales, las colecciones de features son simplemente una lista de features. La estructura de los datos en GeoJSON es siempre un objeto, que consiste en una colección de parejas de nombre/valor (también llamados miembros). Este formato permite representar objetos en el espacio, usando sus coordenadas y añadirles propiedades, que son algo así como los campos de una tabla de atributos.

Continue reading