Python Geospatial Analysis Cookbook by Unknown
Author:Unknown
Language: eng
Format: epub
Publisher: Packt Publishing
How it works...
A quick high-level run-through of what is going on here at the beginning should help clear the air. We have four functions and nine variables within our Python code to split the load of input and output data. The running of our code takes place in the if __name__ == "main": call that is found at the end of the code. We start defining two variables to deal with our inputs that we are going to union together. These two are our input Shapefiles and the other three outputs are GeoJSON and JavaScript files.
The create_shapes()function converts our Shapefile into Shapely MultiPolygon geometry objects. Inside the Python class, the list comprehension is used to generate a new list of polygon objects, which are the input list of polygons used to create our output MultiPolygon. Next, we'll simply run this function passing in our input Shapefiles.
Our create_union() function is up next where we do the real union work. We begin by unioning the two geometry boundaries together that produces a union set of LineStrings and represents the outer bounds of our input polygons. The reason for this is that we do not want to lose the geometries of both polygons, which will, by default, dissolve into one big polygon when simply passed into the Shapely union function. Therefore, we need to rebuild the polygons with the polygonize() Shapely function.
The polygonize function creates a Python generator object, not a simple geometry. This is an iterator that's similar to a list that we need to loop over to get at the individual polygons it's created for us.
We do exactly this in the next code segment using the enumerate() Python function that automatically creates an ID for us for each feature that we use as the id field in the attribute results. After our loop, we use the standard Python json.dump() method to export our newly created GeoJSON file and write it to disk using the Python open() method in the write mode.
Lastly, in our create_union() function, we prepare to output our resulting union polygon as a Shapely MultiPolygon object. This is accomplished simply by looping through the polygonize() iterator and outputting a list that feeds into the Shapely MultiPolygon()function. Finally, we execute the union function, passing in our two input geometries and specifying the output GeoJSON file.
So, we can view our results in our web map as we did in the previous exercise using a small function called write_wkt(). This little function takes the file path to the output JavaScript file that we want to create and the MultiPolygon result's geometry. Shapely then dumps the geometry into the Well Known Text format as we write it out to the JavaScript file.
In the end, a small function called output_geojson_fc() is used to output another GeoJSON file, this time using the Python geojson library. This simply shows you another way to recreate a GeoJSON file. Since GeoJSON is a plain text file, it is possible to create it in many unique ways depending on your personal programming preference.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26522)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19007)
The Fifty Shades Trilogy & Grey by E L James(18961)
Shot Through the Heart by Mercy Celeste(18881)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16986)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16878)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16806)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16701)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16327)
The Subtle Art of Not Giving a F*ck by Mark Manson(14262)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13422)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10924)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9478)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9316)
This is Going to Hurt by Adam Kay(9102)
