Skip to content

Commit 8f8f440

Browse files
authored
Merge pull request #3 from marcbrittain/dev/v0.0.4
update main to v0.0.4
2 parents a7194d8 + d813a22 commit 8f8f440

File tree

6 files changed

+293
-128
lines changed

6 files changed

+293
-128
lines changed

Examples/Example - GeoLineStrings.ipynb

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
"name": "stdout",
7878
"output_type": "stream",
7979
"text": [
80-
"[[ 3. 3. 3.]\n",
81-
" [ 6. 10. 2.]]\n"
80+
"[[3. 3. 3.]]\n"
8281
]
8382
}
8483
],
@@ -365,8 +364,7 @@
365364
"name": "stdout",
366365
"output_type": "stream",
367366
"text": [
368-
"[[2.30084261e+06 1.35167639e+05 1.00000000e+02]\n",
369-
" [2.31208049e+06 7.91798511e+04 1.00000000e+02]]\n"
367+
"[[2.30084261e+06 1.35167639e+05 1.00000000e+02]]\n"
370368
]
371369
}
372370
],
@@ -384,8 +382,7 @@
384382
"name": "stdout",
385383
"output_type": "stream",
386384
"text": [
387-
"[[-71.2 42.5 100. ]\n",
388-
" [-71.3 42. 100. ]]\n"
385+
"[[-71.2 42.5 100. ]]\n"
389386
]
390387
}
391388
],
@@ -449,70 +446,6 @@
449446
"source": [
450447
"gls3.geolinestring_length()"
451448
]
452-
},
453-
{
454-
"cell_type": "markdown",
455-
"id": "4c5eb5a0",
456-
"metadata": {},
457-
"source": [
458-
"# 4. Known Bugs\n",
459-
"\n",
460-
"There is an edge case where if there are multiple coordinates with (x,y) overlap, but different z values, an intersection point may be ignored. In most applications, this may not arise, but in general needs to be addressed and is being actively worked on. The below code demonstrates the bug."
461-
]
462-
},
463-
{
464-
"cell_type": "code",
465-
"execution_count": 18,
466-
"id": "9675a260",
467-
"metadata": {},
468-
"outputs": [],
469-
"source": [
470-
"# Intersection point at [1,1,0] and [4,3,0]\n",
471-
"# There are multiple (x,y) overlap between the coords:\n",
472-
"# 1. [1,1,0]\n",
473-
"# 2. [1,1,5]\n",
474-
"coords5 = [[1,1,0],[4,3,0]]\n",
475-
"coords6 = [[1,1,5],[1,1,0],[4,3,0]]\n",
476-
"\n",
477-
"gls5 = GeoLineString(coords5,is_xy=True)\n",
478-
"gls6 = GeoLineString(coords6,is_xy=True)"
479-
]
480-
},
481-
{
482-
"cell_type": "code",
483-
"execution_count": 19,
484-
"id": "7bf7d05f",
485-
"metadata": {},
486-
"outputs": [
487-
{
488-
"name": "stdout",
489-
"output_type": "stream",
490-
"text": [
491-
"True\n"
492-
]
493-
}
494-
],
495-
"source": [
496-
"print(gls5.intersects(gls6)) # this is okay "
497-
]
498-
},
499-
{
500-
"cell_type": "code",
501-
"execution_count": 20,
502-
"id": "9dbb69e6",
503-
"metadata": {},
504-
"outputs": [
505-
{
506-
"name": "stdout",
507-
"output_type": "stream",
508-
"text": [
509-
"[[4. 3. 0.]]\n"
510-
]
511-
}
512-
],
513-
"source": [
514-
"print(gls5.intersection(gls6)) # this is not correct, [1,1,0] is not identified"
515-
]
516449
}
517450
],
518451
"metadata": {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![downloads](https://img.shields.io/pypi/dm/pygeoshape) ![license](https://img.shields.io/pypi/l/pygeoshape?color=blue) ![pypi](https://img.shields.io/pypi/v/pygeoshape)
22

3-
# PyGeoShape v0.0.3
3+
# PyGeoShape v0.0.4
44
3D extension to [shapely](https://github.com/shapely/shapely/tree/main) and [pyproj](https://github.com/pyproj4/pyproj) to make working with geospatial/trajectory data easier in python. If you found PyGeoShape helpful, please consider adding a star to this repository. Thanks!
55

66
## Getting Started

pygeoshape/geolinestring.py

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from shapely.geometry import LineString
33
from pyproj import Transformer
44
import numpy as np
5+
import numba as nb
56
import matplotlib.pyplot as plt
7+
from pygeoshape.utils import fast_intersection_append
68

79

810
class GeoLineString:
@@ -45,18 +47,18 @@ def __init__(
4547

4648
self.np_coords = np.array(self.xy_coords)
4749
self.length = self.geolinestring_length()
48-
dimensions = len(self.xy_coords[0])
50+
self.dimensions = len(self.xy_coords[0])
4951

5052
# need linestring for each dimension
5153
# x, y, [z]
5254
# x, z, [y]
5355
# y, z, [x]
5456

55-
if dimensions == 2:
57+
if self.dimensions == 2:
5658
self.xy = LineString([(coord[0], coord[1])
5759
for coord in self.xy_coords])
5860

59-
if dimensions == 3:
61+
if self.dimensions == 3:
6062

6163
self.xy = LineString(
6264
[(coord[0], coord[1], coord[2]) for coord in self.xy_coords]
@@ -144,7 +146,7 @@ def intersects(self, geo_obj):
144146
145147
Args:
146148
----
147-
geo_obj (GeoLineString): Second GeoLineString for comparision
149+
geo_obj (GeoLineString): Second GeoLineString for comparison
148150
149151
Returns:
150152
-------
@@ -164,77 +166,63 @@ def intersection(self, geo_obj, lonlat=False):
164166
165167
Args:
166168
----
167-
geo_obj (GeoLineString): Second GeoLineString for comparision
169+
geo_obj (GeoLineString): Second GeoLineString for comparison
168170
lonlat (bool): Format of output coordinates. True returns the coordinates in longitude, latitude. False returns coordinates in x, y
169171
170172
Returns:
171173
-------
172174
intersection (List): Intersection coordinates
173175
174176
"""
175-
xy_check = self.xy.intersects(geo_obj.xy)
176-
xz_check = self.xz.intersects(geo_obj.xz)
177-
yz_check = self.yz.intersects(geo_obj.yz)
178177

179-
intersection = []
178+
intersection = nb.typed.List.empty_list(
179+
nb.types.UniTuple(nb.float64, self.dimensions))
180180

181-
if all([xy_check, xz_check, yz_check]):
181+
inter1 = self.xy.intersection(geo_obj.xy)
182+
inter2 = self.xz.intersection(geo_obj.xz)
183+
inter3 = self.yz.intersection(geo_obj.yz)
182184

183-
inter1 = self.xy.intersection(geo_obj.xy)
184-
inter2 = self.xz.intersection(geo_obj.xz)
185-
inter3 = self.yz.intersection(geo_obj.yz)
185+
if not hasattr(inter1, "geoms"):
186+
xy = np.array(inter1.coords)[0]
187+
n_xy_intersections = 1
186188

187-
if not hasattr(inter1, "geoms"):
188-
xy = list(inter1.coords)[0]
189-
n_xy_intersections = 1
190-
else:
191-
n_xy_intersections = len(inter1.geoms)
192-
193-
if not hasattr(inter2, "geoms"):
194-
xz = list(inter2.coords)[0]
195-
n_xz_intersections = 1
196-
else:
197-
n_xz_intersections = len(inter2.geoms)
198-
199-
if not hasattr(inter3, "geoms"):
200-
yz = list(inter3.coords)[0]
201-
n_yz_intersections = 1
202-
else:
203-
n_yz_intersections = len(inter3.geoms)
204-
205-
for i in range(n_xy_intersections):
206-
207-
for j in range(n_xz_intersections):
208-
209-
for k in range(n_yz_intersections):
189+
else:
190+
n_xy_intersections = len(inter1.geoms)
210191

211-
if hasattr(inter1, "geoms"):
212-
xy = list(inter1.geoms[i].coords)
213-
else:
214-
xy = list(inter1.coords)
192+
if not hasattr(inter2, "geoms"):
193+
xz = np.array(inter2.coords)[0]
194+
n_xz_intersections = 1
195+
else:
196+
n_xz_intersections = len(inter2.geoms)
215197

216-
if hasattr(inter2, "geoms"):
217-
xz = list(inter2.geoms[j].coords)
218-
else:
219-
xz = list(inter2.coords)
198+
if not hasattr(inter3, "geoms"):
199+
yz = np.array(inter3.coords)[0]
200+
n_yz_intersections = 1
201+
else:
202+
n_yz_intersections = len(inter3.geoms)
220203

221-
if hasattr(inter3, "geoms"):
222-
yz = list(inter3.geoms[k].coords)
223-
else:
224-
yz = list(inter3.coords)
204+
for i in range(n_xy_intersections):
225205

226-
for ii in range(len(xy)):
206+
for j in range(n_xz_intersections):
227207

228-
x_y_z = xy[ii]
229-
x_z_y = (x_y_z[0], x_y_z[2], x_y_z[1])
208+
for k in range(n_yz_intersections):
230209

231-
if x_z_y in xz:
210+
if hasattr(inter1, "geoms"):
211+
xy = np.array(inter1.geoms[i].coords)
212+
else:
213+
xy = np.array(inter1.coords)
232214

233-
y_z_x = (x_y_z[1], x_y_z[2], x_y_z[0])
215+
if hasattr(inter2, "geoms"):
216+
xz = np.array(inter2.geoms[j].coords)
217+
else:
218+
xz = np.array(inter2.coords)
234219

235-
if y_z_x in yz:
220+
if hasattr(inter3, "geoms"):
221+
yz = np.array(inter3.geoms[k].coords)
222+
else:
223+
yz = np.array(inter3.coords)
236224

237-
intersection.append(x_y_z)
225+
fast_intersection_append(xy, xz, yz, intersection)
238226

239227
intersection = np.unique(intersection, axis=0)
240228

0 commit comments

Comments
 (0)