Opening the file and getting its contents

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:

  1. Create a new file named geo_functions.py inside the utils directory.
  2. Open the world_areas.py file from Chapter 1, Preparing the Work Environment, and copy the open_shapefile function. Then, paste it into the created file.
  3. Now, change the name of the function to 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.
  4. Now, to keep the code well documented, change the docstring to reflect the function's capabilities. Change it to something similar to "Opens a vector file compatible with OGR, gets the first layer, and returns the OGR data source".
  5. Finally, don't forget to import the required packages. Your code should look similar to this:
    # 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")
  6. Run the code again, and you should see the following output (don't worry about the warnings):
    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

Preparing the content for analysis

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:

  1. Copy this function and paste it into the 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.3.175