Answer To: Spatial Analysis with Python You are required to write a program that performs basic spatial...
Prasun Kumar answered on May 07 2021
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...