-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCensusTools.py
More file actions
57 lines (42 loc) · 1.59 KB
/
CensusTools.py
File metadata and controls
57 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""
CensusTools.py
Author: Anderson Wong
Date: May 08, 2023
Description: This is a Python module that contains general functions for processing
census data:
1. wkttopoly(wkt): a function that takes a WKT polygon geometry and returns it as
a Shapely polygon
2. wktintersect(wkt1, wkt2): a function that takes two WKT polygon geometries and
calculates how much of Polygon1 overlaps with Polygon2
"""
import geojson as geo
import json
from shapely.wkt import loads
from shapely.geometry import mapping, shape
"""
The wkttopoly function takes a WKT polygon geometry and returns it as
a Shapely polygon
"""
def wkttopoly(wkt):
geojson_string = geo.dumps(mapping(loads(wkt)))
geojson = json.loads(geojson_string)
polygon = shape(geojson)
return polygon
"""
The wktintersect function takes two WKT polygon geometries and
calculates how much of Polygon1 overlaps with Polygon2.
Returns the result as a float (a decimal value).
"""
def wktintersect(wkt1, wkt2):
# Converts wkt1 into a Shapely polygon called polygon1
polygon1 = wkttopoly(wkt1)
# Converts wkt2 into a Shapely polygon called polygon2
polygon2 = wkttopoly(wkt2)
# Uses Shapely's intersection function to create a Shapely polygon
# of the intersection of polygon1 and polygon2
intersected = polygon1.intersection(polygon2)
# Divides the area of the intersected polygon by the area of polygon1
percent = intersected.area / polygon1.area
# Return the result
return percent