Database Reference
In-Depth Information
How it works...
Forthisrecipe,youfirstdownloadedandimportedanOSMdatasettoPostGISwith
ogr2ogr
using the GDAL OSM driver.
Then, you created a Python class,
OSMGeocoder
, to provide a very basic support
to the class consumer for geocoding streets names using the OSM data imported
inPostGIS.Forthispurpose,youhaveusedthetrigramssupportincludedinPost-
greSQL with the
pg_trgm contrib
module.
Theclassthatyouhavewrittenismainlycomposedoftwomethods:the
__init__
method, where the connection parameters must be passed in order to instantiate
an
OSMGeocoder
object,andthe
geocode
method.The
geocode
methodaccepts
aninputparameter,
placename
,andcreatesaconnectiontothePostGISdatabase
usingthePsycopg2libraryinordertoexecuteaquerytofindthestreetsinthedata-
base with a name similar to the
placename
parameter.
Theclasscanbeconsumedbothfromthecommandline,usingthe
__name__ ==
'__main__'
code block, or from an external Python code. You tried both the ap-
proaches. In the latter, you created another Python script, where you imported the
OSMGeocoder
classcombinedwiththeGDAL/OGRPythonbindingstogeneratea
newPostGISpointlayerwithfeaturesresultingfromalistofgeocodedstreetnames.