Now, we will open the downloaded file and prepare it for processing. This is something that we already did in Chapter 1, Preparing the Work Environment, so we will copy our function and improve it so that we can reuse it in this application and the ones to come. Here are the steps that we will perform:
geo_functions.py
inside the utils
directory.world_areas.py
file from Chapter 1, Preparing the Work Environment, and copy the open_shapefile
function. Then, paste it into the created file.open_vector_file
so that it makes more sense as we will use this function to open many kinds of file. The geocaching file isn't a shapefile—it's a GPX, and to open it, we don't need to change anything. OGR will handle this for us."Opens a vector file compatible with OGR, gets the first layer, and returns the OGR data source"
.# coding=utf-8 import ogr import osr def open_vector_file(file_path): """Opens an vector file compatible with OGR, get the first layer and returns the ogr datasource. :param str file_path: The full path to the file. :return: The ogr datasource. """ datasource = ogr.Open(file_path) layer = datasource.GetLayerByIndex(0) print("Opening {}".format(file_path)) print("Number of features: {}".format( layer.GetFeatureCount())) return datasource if __name__ == "__main__": open_vector_file("../../data/geocaching.gpx")
Opening ../data/geocaching.gpx Warning 1: Could not parse {2010-10-01T00:00:00Z} as a valid dateTime Warning 1: Could not parse {2011-04-10T00:00:00Z} as a valid dateTime Warning 1: Could not parse {2010-11-21T00:00:00Z} as a valid dateTime Number of features: 130 Warning 1: Could not parse {2010-11-22T00:00:00Z} as a valid dateTime
This application makes use of distances such as meters or miles, so we do not want our measurements to be in degrees. Most geocaching coordinates and point data come in degrees, so we need to convert the coordinate system into the metric system.
To do this, we will start by using a function from Chapter 1, Preparing the Work Environment: transform_geometries
. Perform the following:
geo_functions.py
file. This function will iterate over the features in the data to get its geometry and then convert the coordinate system, returning a list with all of the converted geometries. The function should look similar to this:def transform_geometries(datasource, src_epsg, dst_epsg): """Transform the coordinates of all geometries in the first layer. """ # Part 1 src_srs = osr.SpatialReference() src_srs.ImportFromEPSG(src_epsg) dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(dst_epsg) transformation = osr.CoordinateTransformation(src_srs, dst_srs) layer = datasource.GetLayerByIndex(0) # Part 2 geoms = [] layer.ResetReading() for feature in layer: geom = feature.GetGeometryRef().Clone() geom.Transform(transformation) geoms.append(geom) return geoms
3.141.3.175