Category Archives: GIS

Distance Matrix in R

I’ll do an exercise to make a distance matrix, for that we only need the function spDistN1 from the library “sp”. 

The basic use of the function is the following:

coordinates(meuse) <- c("x", "y")
spDistsN1(meuse, meuse[1,], longlat=TRUE)

The limitation this is it only return the distance of the first element (or given row), because what the function does is to measure the distance of all the elements in relation with only one geometric element (meuse[1, ]) . To solve that we can use the apply function:

apply(meuse@coords, 1, function(x) spDistsN1(meuse@coords, x, longlat = T))

This is a great solution, but the issue is when the data is too big. In that case it will take too much time to process. (Imagine the case I do this with a for loop instead of apply, it would be much slower than now).

To solve the aforementioned problem I must to process with parallelization and as you can guess, the function parApply is just like apply but with parallelization development on it:

m.coord <- meuse@coords
ncore <- detectCores()
cl <- makeCluster(ncore)
clusterExport(cl, c("m.coord"))
clusterEvalQ(cl = cl, expr = c(library(sp)))
parApply(cl = cl, X = m.coord, MARGIN = 1, 
FUN = function(x) spDistsN1(m.coord, x, longlat = T))

Then we have a nice and an efficient solution.

Note: I know it can be easily done with a GIS software, but in this way it
can be incorporated in a bigger and automatized process.


Some Reflextions about Open Source Software

On my time at the University, I learned some of Mapinfo, and Arcview 3.x as a main GIS package, including Avenue as a programming language. Well, and mapobject too.

On my latest years, they acquire a couple of licences of the brand new ArcGIS. When I putted my hands on it, I was a bit excited because the kind of things you can do.

Obviously, the problem with Commercial Softwares is when appears bugs, and the new versions are coming; or whether you need them on your own laptop. So, first of all, I found ArcExplorer, but I didn’t like it, because, you can’t  do much with just a viewer. If you are looking for a viewer, just transform your shapefiles into a kml.

Just by accident, saw a GIS called gvSIG, the problem of this, it was that it used to crush a lot. I’ve heard that know is in a very good shape and is stable. Even on that time, it had some interesting things and for free. I get back to Arcgis, but some time after that I found Quantum GIS, now called renamed as QGIS. I was astonish, for me it was better than ARCGIS, and it’s improving everyday, and you can see how often are new plugins. Well, since that time I’ve been using it.

Being in QGIS itself, I have discovered the existence of GRASS GIS, it’s a bit hard to understand it. His logic it isn’t the same as with others GIS Softwares, but the amazing of it, is you can it with the command console, and as with QGIS, you can use Python with it.

Beside the greats algorithms of GRASS, is the ability of having a connexion with R. Though, I haven’t tried it, I know you can use SAGA with R. Well, there is another tool for Geospatial things, it’s PostGIS (this allow you to deal with geometry), which is an extension of the DBMS called PostgreSQL; another great of this DBMS is pgRouting.

As I come from the GIS world, it isn’t very easy for me, but, I’m learning R to do Spatial Analysis and Data Analysis. It’s amazing what the people have done with R, it’s very fast. And what I love about the open source, is not a black box as tend to occurs with Commercial Software. With an Open Source Software, you can check what algorithms they apply, and it is free.

What I don’t know, is if anyone is doing a development about the Four-step model for transportation planning. If not, we still have to pay for the black box which is TransCAD.

If you’d like to check the things the package you can use on R, I recommend you to follow the next link:

In fact, there are more projects on the open source line. You can check the web page of the GeoDA Center.