moritzmoritzmoritzmoritz commited on
Commit ·
a957001
1
Parent(s): dd265ba
initial commit for logic problem
Browse files
polygons_processing/.ipynb_checkpoints/test_polygons-checkpoint.ipynb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fccd12fbc4544d5a4ff5d00fbca71ce235a46d85b6e5635f903e912b3433c9bd
|
| 3 |
+
size 180861
|
polygons_processing/polygons_test.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Tue May 21 13:44:53 2024
|
| 4 |
+
|
| 5 |
+
@author: morit
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import json
|
| 9 |
+
from shapely.geometry import Polygon
|
| 10 |
+
from shapely.ops import unary_union
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
from matplotlib.patches import Polygon as MplPolygon
|
| 13 |
+
import numpy as np
|
| 14 |
+
import pandas as pd
|
| 15 |
+
|
| 16 |
+
with open('../data_subset/Tree Crown vector datasets-20240520T095639Z-001/Tree Crown vector datasets/manaus_800.geojson', 'r') as file:
|
| 17 |
+
data = json.load(file)
|
| 18 |
+
|
| 19 |
+
print(len(data))
|
| 20 |
+
data_list = data['features']
|
| 21 |
+
print(data['features'][0])
|
| 22 |
+
|
| 23 |
+
# Define the coordinates of the two polygons
|
| 24 |
+
polygon1_coords = data_list[0]['geometry']['coordinates'][0] #[(0, 0),(1,-1), (2, 0), (2, 2), (1, 2)]
|
| 25 |
+
polygon2_coords = data_list[1]['geometry']['coordinates'][0] #[(0.5, -1), (3, 1), (3, 3), (1, 3)]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def add_extreme_coordinates(polygon_data):
|
| 29 |
+
polygon_coords = np.array(data_list[0]['geometry']['coordinates'][0])
|
| 30 |
+
|
| 31 |
+
polygon_data['geometry']['max_lat'] = max(polygon_coords[:,1])
|
| 32 |
+
polygon_data['geometry']['min_lat'] = min(polygon_coords[:,1])
|
| 33 |
+
polygon_data['geometry']['max_lon'] = max(polygon_coords[:,0])
|
| 34 |
+
polygon_data['geometry']['min_lon'] = min(polygon_coords[:,0])
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
for i in range(len(data_list)):
|
| 38 |
+
add_extreme_coordinates(data_list[i])
|
| 39 |
+
|
| 40 |
+
print(data['features'][0])
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Create Polygon objects
|
| 45 |
+
list_polygons = []
|
| 46 |
+
N = 50
|
| 47 |
+
for i in range(N):
|
| 48 |
+
polygon_coords = data_list[i]['geometry']['coordinates'][0] #[(0, 0),(1,-1), (2, 0), (2, 2), (1, 2)]
|
| 49 |
+
polygon = Polygon(polygon_coords)
|
| 50 |
+
list_polygons.append(polygon)
|
| 51 |
+
# Calculate the intersection of the two polygons
|
| 52 |
+
#intersection = polygon1.intersection(polygon2)
|
| 53 |
+
|
| 54 |
+
# Function to plot a polygon
|
| 55 |
+
def plot_polygon(ax, polygon, color, label):
|
| 56 |
+
if not polygon.is_empty:
|
| 57 |
+
x, y = polygon.exterior.xy
|
| 58 |
+
ax.fill(x, y, color=color, alpha=0.5, label=label)
|
| 59 |
+
|
| 60 |
+
# Plot the polygons and their intersection
|
| 61 |
+
fig, ax = plt.subplots()
|
| 62 |
+
|
| 63 |
+
# Plot polygon1
|
| 64 |
+
for i,polygon in enumerate(list_polygons):
|
| 65 |
+
plot_polygon(ax, polygon, 'blue', f"polygon {i}")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# Plot the intersection
|
| 69 |
+
#plot_polygon(ax, intersection, 'red', 'Intersection')
|
| 70 |
+
|
| 71 |
+
# Add legend
|
| 72 |
+
#ax.legend()
|
| 73 |
+
|
| 74 |
+
# Set axis limits
|
| 75 |
+
ax.set_aspect('equal')
|
| 76 |
+
#ax.set_xlim(-1, 4)
|
| 77 |
+
#ax.set_ylim(-1, 4)
|
| 78 |
+
|
| 79 |
+
# Set title
|
| 80 |
+
ax.set_title('Polygons and their Intersection')
|
| 81 |
+
plt.ylabel('lat')
|
| 82 |
+
plt.ylabel('lon')
|
| 83 |
+
|
| 84 |
+
plt.show()
|
| 85 |
+
|
| 86 |
+
df = pd.DataFrame(data_list).drop(columns='type')
|
| 87 |
+
|
| 88 |
+
dict_cols = ['properties', 'geometry']
|
| 89 |
+
for dict_col in dict_cols:
|
| 90 |
+
dict_df = pd.json_normalize(df[dict_col])
|
| 91 |
+
# Merge the new columns back into the original DataFrame
|
| 92 |
+
df = df.drop(columns=[dict_col]).join(dict_df)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
print(dict_df)
|
| 97 |
+
print(df)
|
| 98 |
+
print(df.columns)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# Calculate and print the area of the intersection
|
| 102 |
+
#intersection_area = intersection.area
|
| 103 |
+
#print(f"The area of intersection is: {intersection_area}")
|
polygons_processing/test_polygons.ipynb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fccd12fbc4544d5a4ff5d00fbca71ce235a46d85b6e5635f903e912b3433c9bd
|
| 3 |
+
size 180861
|