import%20marimo%0A%0A__generated_with%20%3D%20%220.23.13%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Spatial%20Data%20(Shapely)%0A%0A%20%20%20%20%5BShapely%5D(https%3A%2F%2Fshapely.readthedocs.io%2F)%20is%20a%20Python%20package%20for%20creating%20and%20manipulating%20geometric%20objects%20such%20as%20points%2C%20lines%2C%20and%20polygons.%20It%20underpins%20much%20of%20the%20Python%20geospatial%20ecosystem%2C%20including%20GeoPandas%2C%20which%20we%20cover%20in%20the%20next%20lesson.%0A%0A%20%20%20%20Points%20represent%20a%20single%20location%2C%20defined%20by%20x%20and%20y%20coordinates%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20shapely.geometry%20import%20LineString%2C%20Point%2C%20Polygon%0A%0A%20%20%20%20pt%20%3D%20Point(0%2C%200)%0A%20%20%20%20pt%0A%20%20%20%20return%20LineString%2C%20Point%2C%20Polygon%2C%20pt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Linestrings%20represent%20sequences%20of%20connected%20points.%20You%20can%20define%20them%20using%20a%20series%20of%20XY%20coordinate%20pairs%20or%20a%20list%20of%20Point%20objects.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LineString)%3A%0A%20%20%20%20line%20%3D%20LineString(%5B(0%2C%205)%2C%20(5%2C%200)%2C%20(10%2C%205)%5D)%0A%0A%20%20%20%20line%0A%20%20%20%20return%20(line%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Polygons%20are%20closed%20shapes%20defined%20by%20a%20sequence%20of%20coordinates.%20Similar%20to%20linestrings%2C%20you%20can%20create%20them%20from%20a%20list%20of%20points%20or%20coordinate%20tuples.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Polygon)%3A%0A%20%20%20%20poly%20%3D%20Polygon(%5B(0%2C%200)%2C%20(0%2C%2010)%2C%20(10%2C%2010)%2C%20(10%2C%200)%5D)%0A%20%20%20%20poly%0A%20%20%20%20return%20(poly%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Shapely%20geometries%20come%20with%20useful%20properties%20that%20provide%20common%20information.%20For%20instance%2C%20you%20can%20easily%20retrieve%20a%20polygon's%20area%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly)%3A%0A%20%20%20%20poly.area%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Similarly%2C%20you%20can%20get%20the%20length%20of%20a%20linestring%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(line)%3A%0A%20%20%20%20line.length%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Other%20handy%20properties%20include%20centroids%2C%20x%20and%20y%20coordinates%2C%20and%20WKT%20(Well-Known%20Text)%20representations%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly)%3A%0A%20%20%20%20poly.centroid%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pt)%3A%0A%20%20%20%20pt.x%2C%20pt.y%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(line)%3A%0A%20%20%20%20line.wkt%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Operations%20and%20Predicates%0A%0A%20%20%20%20Shapely%20supports%20a%20range%20of%20typical%20GIS%20spatial%20operations%20and%20predicates.%20For%20example%2C%20you%20can%20measure%20the%20distance%20between%20two%20spatial%20objects%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(line%2C%20pt)%3A%0A%20%20%20%20pt.distance(line)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Crucially%2C%20Shapely%20operates%20using%20Cartesian%20coordinates.%20For%20accurate%20distance%20calculations%20or%20predicate%20operations%2C%20ensure%20your%20geometries%20are%20in%20a%20projected%20coordinate%20reference%20system%20and%20that%20they%20share%20the%20same%20system.%0A%0A%20%20%20%20The%20Shapely%20documentation%20provides%20comprehensive%20details%20on%20all%20available%20operations%20and%20predicates.%20For%20instance%2C%20the%20%5B%60geometry.Point%60%5D(https%3A%2F%2Fshapely.readthedocs.io%2Fen%2Fstable%2Freference%2Fshapely.Point.html%23shapely.Point)%20page%20describes%20everything%20related%20to%20point%20geometries.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pt)%3A%0A%20%20%20%20pt_buff%20%3D%20pt.buffer(2)%0A%0A%20%20%20%20pt_buff%0A%20%20%20%20return%20(pt_buff%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20As%20an%20example%2C%20the%20documentation%20shows%20that%20the%20%60Point%60%20geometry%20has%20a%20%60buffer%60%20method.%20This%20method%20accepts%20a%20distance%20parameter%20and%20returns%20a%20polygon%20representing%20the%20buffered%20area.%0A%0A%20%20%20%20Common%20spatial%20predicates%20are%20also%20readily%20available.%20For%20example%2C%20you%20can%20use%20the%20%60within%60%20method%20to%20check%20if%20a%20point%20is%20located%20inside%20a%20polygon%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly%2C%20pt)%3A%0A%20%20%20%20pt.within(poly)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20If%20a%20point%20is%20on%20a%20polygon's%20boundary%2C%20%60within%60%20returns%20%60False%60%2C%20but%20%60intersects%60%20or%20%60touches%60%20return%20%60True%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly%2C%20pt)%3A%0A%20%20%20%20pt.intersects(poly)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly%2C%20pt)%3A%0A%20%20%20%20pt.touches(poly)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Buffers%2C%20Unions%2C%20and%20Differencing%0A%0A%20%20%20%20Other%20common%20geometric%20operations%20include%20%60buffer%60.%20For%20instance%2C%20let's%20create%20a%20buffer%20around%20a%20linestring%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(line)%3A%0A%20%20%20%20line_buff%20%3D%20line.buffer(2)%0A%0A%20%20%20%20line_buff%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20You%20can%20use%20the%20%60union%60%20method%20to%20combine%20two%20polygons%20into%20a%20single%2C%20unified%20geometry%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Polygon%2C%20poly)%3A%0A%20%20%20%20poly2%20%3D%20Polygon(%5B(5%2C%205)%2C%20(5%2C%2015)%2C%20(15%2C%2015)%2C%20(15%2C%205)%5D)%0A%0A%20%20%20%20union_poly%20%3D%20poly.union(poly2)%0A%0A%20%20%20%20union_poly%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Operations%20like%20%60difference%60%20also%20behave%20intuitively%2C%20as%20you%20might%20expect%20from%20GIS%20software%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(poly%2C%20pt_buff)%3A%0A%20%20%20%20diff_poly%20%3D%20poly.difference(pt_buff)%0A%0A%20%20%20%20diff_poly%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Some%20operations%20offer%20configurable%20parameters%20for%20finer%20control.%20For%20example%2C%20the%20%60buffer%60%20method%20includes%20%60cap_style%60%20and%20%60join_style%60%20parameters%2C%20which%20control%20the%20appearance%20of%20the%20buffer's%20ends%20and%20joins%20around%20a%20line.%20The%20default%20is%20%60%22round%22%60%3B%20compare%20the%20flat-ended%20version%20below%20with%20the%20rounded%20buffer%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(line)%3A%0A%20%20%20%20buffer_flat%20%3D%20line.buffer(2%2C%20cap_style%3D%22flat%22%2C%20join_style%3D%22mitre%22)%0A%0A%20%20%20%20buffer_flat%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Workflows%0A%0A%20%20%20%20Shapely's%20spatial%20operations%20and%20predicates%20closely%20mirror%20those%20found%20in%20GIS%20software%20like%20QGIS.%20This%20means%20tasks%20you%20might%20perform%20in%20a%20graphical%20user%20interface%20(UI)%20can%20often%20be%20replicated%20in%20Python%2C%20and%20vice-versa.%20Python%20becomes%20particularly%20advantageous%20for%20complex%20or%20lengthy%20workflows%2C%20as%20scripting%20enables%20advanced%20processing%20that%20can%20be%20cumbersome%20or%20difficult%20to%20achieve%20through%20a%20UI.%0A%0A%20%20%20%20Let's%20consider%20a%20simple%20example%3A%20modelling%20streets%20and%20checking%20if%20they%20are%20within%20a%20specified%20distance%20threshold%20of%20particular%20land%20uses.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(LineString)%3A%0A%20%20%20%20street%20%3D%20LineString(%5B(0%2C%200)%2C%20(10%2C%200)%5D)%0A%20%20%20%20street_buffer%20%3D%20street.buffer(5)%0A%0A%20%20%20%20street_buffer%0A%20%20%20%20return%20street%2C%20street_buffer%0A%0A%0A%40app.cell%0Adef%20_(Point)%3A%0A%20%20%20%20land_use%20%3D%20Point(3%2C%204)%0A%20%20%20%20land_use%0A%20%20%20%20return%20(land_use%2C)%0A%0A%0A%40app.cell%0Adef%20_(land_use%2C%20street_buffer)%3A%0A%20%20%20%20is_within%20%3D%20land_use.within(street_buffer)%0A%20%20%20%20is_within%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(land_use%2C%20street)%3A%0A%20%20%20%20distance_to_street%20%3D%20land_use.distance(street)%0A%20%20%20%20distance_to_street%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20real-world%20scenarios%2C%20GeoPandas%20is%20frequently%20used%20for%20such%20workflows.%20It%20excels%20at%20handling%20multiple%20features%2C%20managing%20coordinate%20reference%20systems%2C%20performing%20file%20input%2Foutput%20(I%2FO)%2C%20and%20plotting.%0A%0A%20%20%20%20%23%23%23%20Exercises%0A%0A%20%20%20%20-%20Create%20two%20points%20and%20calculate%20the%20distance%20between%20them.%0A%20%20%20%20-%20Create%20a%20polygon%20and%20check%20if%20a%20point%20is%20inside%20it.%0A%20%20%20%20-%20Create%20two%20polygons%20and%20check%20if%20they%20intersect.%0A%20%20%20%20-%20Create%20a%20LineString%20with%20at%20least%20three%20points.%0A%20%20%20%20-%20Create%20a%20list%20of%20points%20and%20a%20polygon.%20Check%20which%20points%20are%20inside%20the%20polygon.%0A%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20-%20Shapely%20provides%20Python%20objects%20for%20points%2C%20lines%2C%20and%20polygons%20with%20properties%20like%20area%2C%20length%2C%20and%20centroid.%0A%20%20%20%20-%20Spatial%20predicates%20(%60within%60%2C%20%60intersects%60%2C%20%60touches%60)%20test%20relationships%20between%20geometries.%0A%20%20%20%20-%20Operations%20like%20%60buffer%60%2C%20%60union%60%2C%20and%20%60difference%60%20create%20new%20geometries%20from%20existing%20ones.%0A%20%20%20%20-%20Shapely%20uses%20Cartesian%20coordinates%2C%20so%20ensure%20your%20geometries%20share%20the%20same%20projected%20coordinate%20system%20for%20accurate%20results.%0A%20%20%20%20-%20These%20building%20blocks%20underpin%20GeoPandas%2C%20which%20extends%20them%20to%20work%20with%20collections%20of%20features.%0A%0A%20%20%20%20Next%3A%20%5BGeoPandas%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fstart%2F4-geopandas)%20shows%20how%20to%20manage%20and%20analyse%20many%20spatial%20features%20at%20once.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
fb86f6aeb6d9eb475898b7429e72a2e6