Converting a table of addresses to a point layer using gvSIG 2.x

At this post we are going to see a new utility of the geopy library, where we’ll see how to convert a table with addresses to a point layer in gvSIG. The addresses will be searched in Google Maps, so you can search places of interest too (parks, museums…). That layer will be created with the results.

This script will read all the addresses that we have in a table, and they will be converted to coordinates through the geopy library (making requests via Internet, using the Google encoder). They will be added to a new point layer in gvSIG.

The script will keep the rest of the structure of the table if we have more data.

The first step will be to install the GeoPy library if we didn’t have it installed (you can see the instructions at the previous post, Look for your address or location in gvSIG using Scripting!, that includes a video too). Then you have to run gvSIG and create the script.

Once the new script has been created we have to copy this source code on it:

from gvsig import *
from geom import *
from geopy.geocoders import get_geocoder_for_service
def getGeometryType(type, subtype):
 geometryManager = GeometryLocator.getGeometryManager()
 return geometryManager.getGeometryType(type,subtype)
def main(*args):
    """ Add the address to point layer """
    ### Initial data
    fieldAddress = "Address" #Field of the table
    path = "/home/myuser/gis/point_layer.shp"
    ### Process
    #We copy the scheme of the table at the layer + geometry
    layer = currentTable()
    schema = layer.getSchema()
    newSchema = createSchema(schema)
    newSchema.append("GEOMETRY", "GEOMETRY")
    CRS = currentView().getProjection()
    output = createShape(newSchema, path, CRS=CRS, geometryType=POINT)

    # Used locator: Google v3
    geolocator = get_geocoder_for_service("googlev3")

    #The table is read
    for feature in layer.features():
        address = feature.get(fieldAddress)
        #..converting a location
        location = geolocator().geocode(address)
        values = feature.getValues()
            print location.longitude, location.latitude
            #..creating a point with the values of the geolocator
            point = createPoint(location.longitude, location.latitude)
            #..creating the geometry and adding the new layer 

#adding the new layer to the view and finishing editing.

Important: We have to modify these parameters at the script, at this text: “### Initial data”:

  • We specify the name of the field that contains the addresses or type of locations. It will be done modifying the fieldAddress variable, changing “Address” by the name of the field of our table.
  • We change the path where the new layer will be created and its name, modifying the ruta variable.

Once the Script is created and saved, we will open a table that contains a field with the addresses or type of locations.

Now, before running it, we have to take into account that we have to have the table opened in the forefront, and a View behind it. That’s because at the script we are referring to the active table.

The script, has some limitations because it depends on the online service offered by Google (we also can use other encoders, listed at geopy website), but it has other advantages. For example, we can search by fields that are not postal addresses, like “Guggenheim Museum”, city names, countries…

You can find this script and many more at the gvSIG Outreach.

If you have any doubt you can write here or use the mailing lists.

And if you are interested in creating your own scripts, there’s a free Mooc (in Spanish at this moment).

We hope it’s useful for you!

This entry was posted in development, english, gvSIG Desktop, scripting and tagged . Bookmark the permalink.

One Response to Converting a table of addresses to a point layer using gvSIG 2.x

  1. Pingback: Converting a table of addresses to a point layer using gvSIG 2.x |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s