Leaflet with browserify: basic tutorial

This post will will show you how to build a very simple application that uses leaflet js with browserify.
http://browserify.org/

why do you want to do that?

Browserify will let us use node modules on the browser side (and there is like 255000 total packages to choose on npm) , on NPM you will find lot’s of client-side packages like JQuery, and of course, our one and only LeafletJS. Also, it let you separate your own code on modules, making it more easy to maintain especially when you end up with a very large JavaScript project.

Continue reading

Setting Up a Geospatial Python tool box with conda

Anaconda

I’m using Ubuntu 15.04 and it comes with Python 2.7 and Python 3.4 pre-installed. Installing into this Python installation requires root access to the system and it may be better to use a virtual environment when installing packages with pip, which is a little confusing. Also, if you are on a Windows machine installing packages may be complicated.

To me the easier solution to setting up a geospatial environment  is to use Conda (a open source,  cross platform package manager application that installs, runs, and updates packages and their dependencies) and doesn’t require administrator privileges to install anything.

To install Conda, you will download Anaconda or Miniconda (both are free). Anaconda is a Python distribution that  includes the most popular Python packages for science, maths, engineering, and data analysis, while Miniconda is just a conda and python installation.

Continue reading

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

Install postgis 2.1 and QGIS on ubuntu 15.04

This is tested on a fresh installation of Ubuntu 15.04.


$ sudo apt-get install postgresql postgresql-contrib postgis postgresql-9.4-postgis-2.1 pgadmin3

# add a new user (need to be authenticated as a superuser)
$ sudo -u postgres createuser --superuser mysuperuser

# Set password for created user
$ sudo -u postgres psql postgres
postgres=# \password mysuperuser

Enter new password:mysuperuserpassword
Enter it again:

# create password for user postgre
$ sudo su
$ su - postgres

Now you can access the PostgreSQL prompt with the command:
$ psql

And then change the password for postgres role by typing :
$ \password postgres
Enter new password: postgres

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