Book Image

Geospatial Development By Example with Python

By : Pablo Carreira
5 (1)
Book Image

Geospatial Development By Example with Python

5 (1)
By: Pablo Carreira

Overview of this book

From Python programming good practices to the advanced use of analysis packages, this book teaches you how to write applications that will perform complex geoprocessing tasks that can be replicated and reused. Much more than simple scripts, you will write functions to import data, create Python classes that represent your features, and learn how to combine and filter them. With pluggable mechanisms, you will learn how to visualize data and the results of analysis in beautiful maps that can be batch-generated and embedded into documents or web pages. Finally, you will learn how to consume and process an enormous amount of data very efficiently by using advanced tools and modern computers’ parallel processing capabilities.
Table of Contents (17 chapters)
Geospatial Development By Example with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using Shapely to handle geometries


Shapely is a Python package for the analysis of planar features. It uses functions from the GEOS library and a port of the Java Topology Suite (JTS).

It has mainly the same classes and functions as OGR while dealing with geometries. Although it's not a replacement for OGR, it has a more pythonic and a very intuitive interface, it is better optimized, and it has a well-developed documentation.

To make things clear, Shapely is intended to analyze geometries and only geometries. It does not handle features' attributes, neither is it capable of reading and writing geospatial files.

For a direct comparison of Shapely and OGR, we are going to rewrite the previous examples:

  1. Add the following lines to the wkt_experiments.py file (you can keep or remove the previous code, it's up to you):

    from shapely.geometry import Polygon
    
    print('Examples with Shapely')
    polygon1 = Polygon([(1, 1), (1, 9), (8, 9), (8, 1), (1, 1)])
    print(polygon1.__class__)
    print(polygon1.area)
    
    polygon2...