Spatial Analysis with Python You are required to write a program that performs basic spatial analysis on a Polygon feature set and Point feature set. You should get these from Geoserver in GeoJSON...

1 answer below »

Spatial Analysis with Python


You are required to write a program that performs basic spatial analysis on aPolygonfeature set andPointfeature set. You should get these fromGeoserverin GeoJSON format. I haven't specified which layers to use - I'll leave that as a choice for you. There is a selection of datasets onhttp://193.1.33.31:8081/geoserver. Look for layers in thecensus2011,census2016andTUDublinworkspaces. Consider which point and polygon datasets would match.


Specifically, we want to do the following:




  1. Create a single polygon from the Union of all the polygons.




  2. Compute the centroid of the single polygon.




  3. Extract the points that lie within the single polygon.




  4. Compute a convex hull and centroid for the extracted points




  5. Compute the distance between the centroid of the single polygon and the centroid of the points that lie within the single polygon.




  6. Create a representation of the line joining the two centroids




  7. Geocode both centroids and add their names to the appropriate point as an attribute




  8. Createshapefilesto store the results of the above. Bear in mind that a shapefile contains a single geometry type and is a set of thematically related features. Therefore you will need to create shapefiles as follows:




    • Combined polygon from Union




    • Points that lie within Combined Polygon




    • Convex hull of the points from above




    • Both centroids. Each should have an attribute to hold its name returned from the geocoding process.




    • Linestring representing the distance between the centroids






You should also create a graphical user interface (GUI) to facilitate interaction with your program. This should facilitate the selection of the source server and datasets and present asummaryof your results.


The above is not now necessary, as we haven't covered GUI programming and it's unlikely, give the current situation, that we will.


The user can then use any GIS application such asArcGISorQGISto display the resulting shapefiles.


To get you started, I have created some useful Python modules in theutilitiesfolder onGitHub. Among other things, these have functions which implement the process of accessing GeoJSON data from Geoserver. Get these and study how they work. You should be able to import any of these directly into your program.


Marking Scheme


An indicative marking scheme would be:




  • Functionality (40%)




    • the extent to which the solution meets the brief






  • Readability (20%)




    • the extent to which the solution is nicely laid out and commented






  • Elegance/Style (20%)




    • the extent to which the solution is implemented elegantly. Implies simplicity with completeness.






  • Language features (10%)




    • use and choice of appropriate techniques from the alternatives offered by the language(s)






  • Innovation (10%)




    • unexpected, creative or innovative code demonstrated by the student.






Obviously, there's a degree of flexibility within these. For example, a student mightn't demonstrate much innovation but might implement a very elegant solution so should be rewarded accordingly.


Marks awarded for any reasonable effort even if you don't achieve a complete solution. Remember, we're not trying to create a commercial product, it's areasonablychallenging learning exercise

Answered Same DayMay 03, 2021

Answer To: Spatial Analysis with Python You are required to write a program that performs basic spatial...

Prasun Kumar answered on May 07 2021
156 Votes
steven2/.cache/line_joining_centroids.cpg
UTF-8
steven2/.cache/line_joining_centroids.dbf
        id        1
steven2/.cache/line_joining_centroids.prj
PROJCS["TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/line_joining_centroids.shp
steven2/.cache/line_joining_centroids.shx
steven2/.cache/point_centroid_of_convex_hull.cpg
UTF-8
steven2/.cache/point_centroid_of_convex_hull.dbf
        longitude        latitude        geocoded_a        -8.88        52.01        Co Cork, Cork
steven2/.cache/point_centroid_of_convex_hull.prj
PROJCS["
TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/point_centroid_of_convex_hull.shp
steven2/.cache/point_centroid_of_convex_hull.shx
steven2/.cache/point_centroid_of_union.cpg
UTF-8
steven2/.cache/point_centroid_of_union.dbf
        longitude        latitude        geocoded_a        -9.18        52.00        Co Kerry, Kerry
steven2/.cache/point_centroid_of_union.prj
PROJCS["TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/point_centroid_of_union.shp
steven2/.cache/point_centroid_of_union.shx
steven2/.cache/points_lying_in_union.cpg
UTF-8
steven2/.cache/points_lying_in_union.dbf
        population        6868
        22941
        8891
        9495
        7300
        145502
        10000
        399802
        190384
        10501
        14775
        5492
        1246088
        119230
steven2/.cache/points_lying_in_union.prj
PROJCS["TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/points_lying_in_union.shp
steven2/.cache/points_lying_in_union.shx
steven2/.cache/poly_convex_hull.cpg
UTF-8
steven2/.cache/poly_convex_hull.dbf
        id        1
steven2/.cache/poly_convex_hull.prj
PROJCS["TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/poly_convex_hull.shp
steven2/.cache/poly_convex_hull.shx
steven2/.cache/polys_from_union.cpg
UTF-8
steven2/.cache/polys_from_union.dbf
        nuts3name        south-west
steven2/.cache/polys_from_union.prj
PROJCS["TM65_Irish_Grid",GEOGCS["GCS_TM65",DATUM["D_TM65",SPHEROID["Airy_Modified_1849",6377340.189,299.3249646]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",53.5],PARAMETER["central_meridian",-8],PARAMETER["scale_factor",1.000035],PARAMETER["false_easting",200000],PARAMETER["false_northing",250000],UNIT["Meter",1]]
steven2/.cache/polys_from_union.shp
steven2/.cache/polys_from_union.shx
steven2/.idea/.gitignore
# Default ignored files
/shelf/
/workspace.xml
steven2/.idea/inspectionProfiles/profiles_settings.xml





steven2/.idea/misc.xml


steven2/.idea/modules.xml






steven2/.idea/steven2.iml






steven2/.idea/workspace.xml



















































1588681231093


1588681231093





























steven2/CA2_2020_steven.py
"""
Description:
Code for GISP 2020 Assignment (Spatial Analysis with Python)
Specific tasks:
1. Create a single polygon from the Union of all the polygons.
2. Compute the centroid of the single polygon.
3. Extract the points that lie within the single polygon.
4. Compute a convex hull and centroid for the extracted points
5. Compute the distance between the centroid of the single polygon and the centroid of the points that lie within the
single polygon.
6. Create a representation of the line joining the two centroids
7. Geocode both (7.1 and 7.2) centroids and add their names to the appropriate point as an attribute
8. Create shapefiles to store the results of the above.
Author: Steven Kenny
Date: 7 May 2020
"""
# ##########################
# import required packages
# We use this to enable us to read GeoJSON files from the Net
import utilities.read_from_file_or_net as rfn
# Used to check whether .cache directory exists. Else create.
import utilities.get_temp_dir as gtd
# 'shape' creates a geometry object from a GeoJSON-like structure that Fiona gives you, 'mapping' creates a GeoJSON-like
# structure that Fiona needs rom a Shpely geometry object. They are, therefore opposites -> convert a to b and b to a.
from shapely.geometry import shape, mapping, MultiPoint, Point
# This is the shapely operation that actually does the merge computation
from shapely.ops import unary_union
# So that we can read and write shapefiles...
import fiona
from fiona import transform
from fiona.crs import from_epsg
# Deal with serializing from/to JSON
import json
# Geocoding an address or reverse geocode a coordinate pair is done using ArcGIS API
# Using urllib to call the API & also to check if URL exists
from urllib.request import Request, urlopen
# ##########################
# The output files that will be created from this program
# As they are 'sacrificial' - we are putting it in the .cache directory
DEST_FILE_POLY_FROM_UNION = ".cache/polys_from_union.shp"
DEST_FILE_POINT_CENTROID_OF_UNION = ".cache/point_centroid_of_union.shp"
DEST_FILE_POINTS_LYING_IN_UNION = ".cache/points_lying_in_union.shp"
DEST_FILE_POLY_CONVEX_HULL = ".cache/poly_convex_hull.shp"
DEST_FILE_POINT_CENTROID_OF_CONVEX_HULL = ".cache/point_centroid_of_convex_hull.shp"
DEST_FILE_LINE_JOINING_CENTROIDS = ".cache/line_joining_centroids.shp"
# Ensuring .cache directory exists.
gtd.make_temp_dir('_')
# ##########################
def get_stuff_from_geoserver(stuff):
poly_search_attribute = "nuts3name"
poly_search_value = "south-west"
point_search_attribute = "population"
point_search_value = 5000
print("Downloading data from Geoserver. This may take a few seconds...")
geo_files = {}
for k,v in stuff.items():
geo_files[k] = json.loads(rfn.get_stuff_from_net(v))
print("Data downloaded!")
print("Filtering counties/polygons for search criteria")
filtered_polys = []
for feature in geo_files["counties_url"]["features"]:
if poly_search_value in feature["properties"][poly_search_attribute].lower():
filtered_polys.append(feature)
print("{} polygons found matching criteria".format(len(filtered_polys)))
print("Filtering geonames/points for search criteria")
filtered_points = []
for point in geo_files["geonames_url"]["features"]:
if point["properties"][point_search_attribute] >= point_search_value:
filtered_points.append(point)
print("{} points found matching...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here