In an previous post, we spoke about the utility of the “geopy” library for Geocoding in gvSIG 2 using scripting.
Now, with the next script we are going to search addresses or towns and gvSIG will zoom to that point.
The first step will be to install the GeoPy library in our computer. For that we have to download the “geopy-1.7.1-patched-for-gvSIG-2.1.0.zip” file and unzip it in the folder:
at the home folder of our user (in that lib folder we will have the geopy and simplejson folders finally). If we had gvSIG opened we have to close it and run it again.
Depending on the operating system, we can find that gvSIG folder in:
- Linux: /home/MY_USER
- Windows: C:\Document and Settings\MY_USER or C:\Users\MY_USER
We remember that this library uses the Google addresses browser. It also has its defects like confusions in addresses or the necessity to have an online connection.
Once the libraries are installed, we will run gvSIG, and we will go to the Scripting Composer (Tools->Scripting->Scripting Composer menu), we will create a script (File->New), with the name that we want (for example ‘Address browser’), and we will add this source code on it:
from gvsig import * from geopy.geocoders import get_geocoder_for_service from commonsdialog import * from geom import * def main(*args): # Google v3: from address to coordinates address = inputbox("Search Address", "Search Address",1) geolocator = get_geocoder_for_service("googlev3") location = geolocator().geocode(address) print"Address: ", (location.address), "Coordinates: ", ((location.latitude, location.longitude)) geomPoint = createPoint(location.longitude, location.latitude,) centerView(currentView(), geomPoint.buffer(0.0005)) def centerView(view, geom): view.getMap().getViewPort().setEnvelope(geom.getEnvelope())
Then we will open a View in gvSIG, that will be in geographical coordinates on WGS84 (EPSG: 4326) for the correct working, because they are the coodinates that the library gives.
On the View, with the layers included, we will open the scripting launcher (Tools->Scripting->Scripting Launcher menu), and we will run the script that we have created (doble-clicking on it). Once the script has been run, it will ask for the address or town through a text box, and it will center the View to the point finally.
We hope you enjoy this tool!