-
Notifications
You must be signed in to change notification settings - Fork 1
/
npg_clip.py
1225 lines (1117 loc) · 45.2 KB
/
npg_clip.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# noqa: D205, D400, F403, F401
r"""
---------
npg_clip
---------
----
Script :
npg_clip.py
Author :
`<https://github.com/Dan-Patterson>`_.
Modified :
2022-03-04
Example
-------
Put example here.
"""
# pylint: disable=C0103,C0302,C0415
# pylint: disable=E1101,E1121
# pylint: disable=W0105,W0201,W0212,W0221,W0611,W0612,W0621
# pylint: disable=R0902,R0904,R0912,R0913,R0914,R0915
import sys
import math
# from textwrap import dedent
import numpy as np
# -- optional numpy imports
# from numpy.lib.recfunctions import structured_to_unstructured as stu
# from numpy.lib.recfunctions import unstructured_to_structured as uts
# from numpy.lib.recfunctions import repack_fields
import npg
from npg import npg_plots
from npg.npg_plots import plot_polygons
# from npg.npg_utils import time_deco
ft = {"bool": lambda x: repr(x.astype(np.int32)),
"float_kind": '{: 6.2f}'.format}
np.set_printoptions(
edgeitems=10, linewidth=120, precision=3, suppress=True, threshold=200,
formatter=ft
)
script = sys.argv[0] # print this should you need to locate the script
__all__ = ['clip', 'common_extent', 'roll_arrays', 'split_seq']
__helpers__ = ['_concat_', '_is_pnt_on_line_']
__all__ = __helpers__ + __all__
# ---- (1) general helpers
#
def _is_pnt_on_line_(start, end, xy, tolerance=1.0e-12):
"""Perform a distance check of whether a point is on a line.
eps = 2**-52 = 2.220446049250313e-16
np.finfo(float).eps = 2.220446049250313e-16
np.finfo(float)
finfo(resolution=1e-15, min=-1.7976931348623157e+308,
max=1.7976931348623157e+308, dtype=float64)
"""
#
def dist(a, b):
"""Add math.sqrt() for actual distance."""
return np.sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2)
#
line_leng = dist(start, end)
if tolerance == 0.0:
return dist(start, xy) + dist(end, xy) == line_leng
else:
d = (dist(start, xy) + dist(end, xy)) - line_leng
return -tolerance <= d <= tolerance
def _roll_(ar, num=1):
"""Roll coordinates by `num` rows so that row `num` is in position 0."""
return np.concatenate((ar[num:-1], ar[:num], [ar[num]]), axis=0)
def common_extent(a, b):
"""Return the extent overlap for two polygons as L, B, R, T or None."""
ext0 = np.concatenate((np.min(a, axis=0), np.max(a, axis=0)))
ext1 = np.concatenate((np.min(b, axis=0), np.max(b, axis=0)))
es = np.concatenate((ext0[None, :], ext1[None, :]), axis=0)
maxs = np.max(es, axis=0)
mins = np.min(es, axis=0)
L, B = maxs[:2]
R, T = mins[2:]
if (L <= R) and (B <= T):
return (True, np.array([L, B, R, T])) # (x1, y1, x2, y2)
return (False, None)
def roll_arrays(arrs):
"""Roll point coordinates to a new starting position.
Notes
-----
Rolls the coordinates of the Geo array attempting to put the start/end
points as close to the lower-left of the ring extent as possible.
"""
# --
def _LL_(arr):
"""Return the closest point to the lower left of the polygon."""
LL = np.min(arr, axis=0)
idx = (np.abs(arr - LL)).argmin(axis=0)
return idx[0]
# --
if not isinstance(arrs, (list, tuple)):
print("List/tuple of arrays required.")
return
out = []
for ar in arrs:
num = _LL_(ar)
out.append(np.concatenate((ar[num:-1], ar[:num], [ar[num]]), axis=0))
return out
def uniq_1d(arr):
"""Return mini 1D unique for sorted values."""
arr.sort()
mask = np.empty(arr.shape, dtype=np.bool_)
mask[:1] = True
mask[1:] = arr[1:] != arr[:-1]
return arr[mask]
def uniq_2d(arr, return_sorted=False):
"""Return mini `unique` for 2D coordinates. Derived from np.unique.
Notes
-----
For returning in the original order this is equivalent to::
u, idx = np.unique(x_pnts, return_index=True, axis=0)
x_pnts[np.sort(idx)]
"""
def _reshape_uniq_(uniq, dt, shp):
n = len(uniq)
uniq = uniq.view(dt)
uniq = uniq.reshape(n, *shp[1:])
uniq = np.moveaxis(uniq, 0, 0)
return uniq
arr = np.asarray(arr)
shp = arr.shape
dt = arr.dtype
st_arr = arr.view(dt.descr * shp[1])
ar = st_arr.flatten()
if return_sorted:
perm = ar.argsort(kind='mergesort')
aux = ar[perm]
else:
# ar.sort(kind='mergesort') # removed
aux = ar
mask = np.empty(aux.shape, dtype=np.bool_)
mask[:1] = True
mask[1:] = aux[1:] != aux[:-1]
ret = aux[mask]
uniq = _reshape_uniq_(ret, dt, shp)
if return_sorted: # return_index in unique
return uniq, perm[mask]
return uniq
def to_struct(whr, x_pnts, as_view=True):
"""Return structured array for intersections.
Use z.reshape(z.shape[0], 1) to view in column format.
"""
dt = [("p_seg", "i8"), ("c_seg", "i8"), ("x", "f8"), ("y", "f8")]
z = np.empty((whr.shape[0],), dtype=dt)
z["p_seg"] = whr[:, 0]
z["c_seg"] = whr[:, 1]
z["x"] = x_pnts[:, 0]
z["y"] = x_pnts[:, 1]
if as_view:
return z.reshape(z.shape[0], 1)
return z
def reverse_whr(whr, x_pnts, return_pnts=True):
"""Return the reverse of the intersections of the `whr` array.
Parameters
----------
whr : array-like
The intersection ids of the clipper/poly.
x_pnts : array
The intersection points of the two polygons in the `whr` order.
Returns
-------
Returns the new `whr` and `crossings` arrays for both incarnations of the
polygon intersections. Optionally, the intersection points are returned.
"""
def _crossings_(w):
"""Return the crossing array."""
s, r = divmod(w.shape[0], 2) # check for even pairing
if r == 0:
x = (w.reshape(-1, 4)).copy()
x[:, 1], x[:, 2] = x[:, 2], x[:, 1].copy()
else:
x = (w[:s * 2].reshape(-1, 4)).copy()
x[:, 1], x[:, 2] = x[:, 2], x[:, 1].copy()
lstx, lsty = w[s * 2:][0]
x = np.concatenate((x, np.array([[lstx, -1, lsty, -1]])), axis=0)
crossings = x.copy()
return crossings
# --
cross1 = _crossings_(whr)
idx = np.lexsort((whr[:, 0], whr[:, 1]))
whr2 = whr[idx].copy()
whr2.T[[0, 1]] = whr2.T[[1, 0]]
cross2 = _crossings_(whr2)
if return_pnts:
x_pnts2 = x_pnts[idx]
return whr, cross1, x_pnts, whr2, cross2, x_pnts2
return whr, cross1, whr2, cross2
# ---- (2) winding number, intersections
#
def _wn_clip_(pnts, poly, all_info=True):
"""Return points in a polygon or on its perimeter, using `winding number`.
Parameters
----------
pnts, poly : array_like
Geometries represent the points and polygons. `pnts` is assumed to be
another polygon since clipping is being performed.
all_info : boolean
True, returns points in polygons, the in and out id values, the
crossing type and winding number. False, simply returns the winding
number, with 0 being outside points and -1 being inside points for a
clockwise-oriented polygon.
Notes
-----
Negative and positive zero np.NZERO, np.PZERO == 0.0.
Other
-----
z = np.asarray(np.nonzero(npg.eucl_dist(a, b) == 0.)).T
a[z[:, 0]] and b[z[:, 1]] return the points from both arrays that have a
distance of 0.0 and they intersect
"""
def _w_(a, b, all_info):
"""Return winding number and other values."""
x0, y0 = a[:-1].T # point `from` coordinates
# x1, y1 = a[1:].T # point `to` coordinates
x1_x0, y1_y0 = (a[1:] - a[:-1]).T
#
x2, y2 = b[:-1].T # polygon `from` coordinates
x3, y3 = b[1:].T # polygon `to` coordinates
x3_x2, y3_y2 = (b[1:] - b[:-1]).T
# reshape poly deltas
x3_x2 = x3_x2[:, None]
y3_y2 = y3_y2[:, None]
# deltas between pnts/poly x and y
x0_x2 = x0 - x2[:, None]
y0_y2 = y0 - y2[:, None]
#
a_0 = y0_y2 * x3_x2
a_1 = y3_y2 * x0_x2
b_0 = y0_y2 * x1_x0
b_1 = y1_y0 * x0_x2
a_num = a_0 - a_1
b_num = b_0 - b_1
#
# pnts in poly
chk1 = (y0_y2 >= 0.0) # y above poly's first y value, per segment
chk2 = np.less(y0, y3[:, None]) # y above the poly's second point
chk3 = np.sign(a_num).astype(np.int32)
pos = (chk1 & chk2 & (chk3 > 0)).sum(axis=0, dtype=np.int32)
neg = (~chk1 & ~chk2 & (chk3 < 0)).sum(axis=0, dtype=np.int32)
wn_vals = pos - neg
wn_ = np.concatenate((wn_vals, np.array([wn_vals[0]])))
#
if all_info:
denom = (x1_x0 * y3_y2) - (y1_y0 * x3_x2)
return wn_, denom, x0, y0, x1_x0, y1_y0, a_num, b_num
return wn_
def _xsect_(a_num, b_num, denom, x1_x0, y1_y0, x0, y0):
"""Return the intersection."""
with np.errstate(all="ignore"): # ignore all errors
u_a = a_num / denom
u_b = b_num / denom
z0 = np.logical_and(u_a >= 0., u_a <= 1.) # np.isfinite(u_a)`
z1 = np.logical_and(u_b >= 0., u_b <= 1.) # np.isfinite(u_b)
both = (z0 & z1)
xs = (u_a * x1_x0 + x0)[both]
ys = (u_a * y1_y0 + y0)[both]
x_pnts = []
if xs.size > 0:
x_pnts = np.concatenate((xs[:, None], ys[:, None]), axis=1)
whr = np.array(np.nonzero(both)).T
return whr, x_pnts
# --
# Use `_w_` and `_xsect_` to determine pnts in poly
wn_, denom, x0, y0, x1_x0, y1_y0, a_num, b_num = _w_(pnts, poly, True)
whr, x_pnts = _xsect_(a_num, b_num, denom, x1_x0, y1_y0, x0, y0)
p_in_c = np.nonzero(wn_)[0]
# p_out_c = np.nonzero(wn_ + 1)[0]
x_type = np.concatenate((wn_[:-1, None], wn_[1:, None]), axis=1)
#
wn2_ = _w_(poly, pnts, False) # get poly points in other geometry
c_in_p = np.nonzero(wn2_)[0]
# c_out_p = np.nonzero(wn2_ + 1)[0]
vals = [x_pnts, p_in_c, c_in_p, x_type, whr]
# if ..outs needed [x_pnts, p_in_c, c_in_p, c_out_p, ...wn_, whr]
if all_info:
return vals
return whr # wn_
# ---- (3) polygon clipping
# -- helpers
def _concat_(x):
"""Concatenate array pieces."""
z1 = [np.atleast_2d(i) for i in x if len(i) > 0]
return np.concatenate(z1, axis=0)
def _dist_(a, b):
"""Return squared distance. Add math.sqrt() for actual distance."""
return np.sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2)
def _a_eq_b_(a, b, atol=1.0e-8, rtol=1.0e-5, return_pnts=False):
"""
Return the points in `b` that are equal to the points in `a`.
Parameters
----------
a, b : ndarrays
The arrays must have ndim=2, although their shapes need not be equal.
See Also
--------
See `npg_helpers.a_eq_b` for details.
"""
a = np.atleast_2d(a)
b = np.atleast_2d(b)
if b.size > 2:
b = b[:, None]
w = np.less_equal(np.abs(a - b), atol + rtol * abs(b)).all(-1)
if w.ndim > 1:
w = w.any(-1).squeeze()
if return_pnts:
return b[w].squeeze()
return w.squeeze() # w.any(0).any()
def _eq_(p_0, c_0):
"""Return whether the poly and clp points are equal."""
v0, v1 = p_0[c_0] - c_0[p_0]
return math.isclose(v0, v1)
def _before_chk_(poly, seen_, id_, a_in_b):
"""Return the points which are before the point in question.
Parameters
----------
poly : array_like
The polygon to check.
seen_ : list
The id values already to seen.
id_ : integer
The id value to check
a_in_b : list
"""
lst = seen_ + [id_]
strt = lst[0] if len(lst) > 1 else 0
end_ = lst[-1] + 1
bf = [x for x in range(strt, end_) if x not in lst]
if len(bf) > 0:
bf = sorted(list(set(a_in_b).intersection(bf)))
pnts = [poly[i] for i in bf]
if pnts:
return bf, pnts
return bf, None
def _btw_chk_(a, b, in_, seen): # include_ends=True):
"""Return points which are between/within polygon ids `a` and `b`.
Notes
-----
If `a` > `b`, then any found points will be returned in reverse order.
Example
-------
Order of entities seen is important so they are returned in that order.
>>> _btw_chk_(a=10, b=0, in_=[3, 4, 5], seen=[3])
... [5, 4]
>>> _btw_chk_(a=0, b=10, in_=[3, 4, 5], seen=[3])
... [4, 5]
"""
if a > b:
a, b = b, a
btw = set(range(a, b + 1))
if len(btw) > 0:
s = set(in_).difference(set(seen))
ids = btw.intersection(s) # or [i for i in btw if i in s]
if ids:
return sorted(list(ids))
return []
def _to_add_(XCsub, sub_, ply_seen, clp_seen, tot_):
"""Simplify updating ply_seen, clp_seen and tot_ arrays."""
c_0, c_1, p_0, p_1 = XCsub[:4]
clp_seen = list(set(clp_seen).union([c_0, c_1]))
ply_seen = list(set(ply_seen).union([p_0, p_1]))
tot_.append([XCsub.ravel(), sub_])
return ply_seen, clp_seen, tot_
def nodes(p_in_c, c_in_p, clp, poly, x_pnts):
"""Return node intersection data. `_cpx_ ,clipper polygon intersection`.
Parameters
----------
p_in_c, c_in_p : lists
Id values of points in poly and clipper respectively.
clp, poly : array_like
The geometry of the clipper and the polygon being clipped.
x_pnts : array_like
The intersection points of the geometry edges.
Returns
-------
- p_in_c : polygon points in clipper and reverse
- c_in_p : clipper points in polygon and those that are equal
- c_eq_p, p_eq_c : clipper/polygon equality
- c_eq_x, p_eq_x : intersection points equality checks for both geometries
- cp_eq, cx_eq, px_eq : clipper, polygon and intersection equivalents
Notes
-----
Forming a dictionary for cp_eq, cs_eq, px_eq::
kys = uniq_1d(px_eq[:, 0]).tolist() # [ 0, 2, 3, 4, 12]
dc = {} # -- dictionary
dc[0] = px_eq[px_eq[:, 0] == 0][:,1].tolist()
for k in kys:
dc[k] = px_eq[px_eq[:, 0] == k][:,1].tolist()
dc
{0: [0, 3, 13, 14],
2: [7, 8],
3: [9, 10, 11],
4: [1, 2, 5, 6],
12: [0, 3, 13, 14]}
Or, you can split the array::
whr = np.nonzero(np.diff(cx_eq[:, 0]))[0] + 1
np.array_split(cx_eq, whr)
[array([[ 0, 0],
[ 0, 3],
[ 0, 13],
[ 0, 14]], dtype=int64),
array([[1, 1],
[1, 2],
[1, 5],
[1, 6]], dtype=int64),
array([[2, 4]], dtype=int64),
array([[3, 7],
[3, 8]], dtype=int64),
array([[ 4, 9],
[ 4, 10],
[ 4, 11]], dtype=int64),
array([[ 6, 0],
[ 6, 3],
[ 6, 13],
[ 6, 14]], dtype=int64)]
# -- Point equality check. -- c_eq_p, c_eq_x, p_eq_c, p_eq_x
# poly[p_eq_c], poly[p_eq_x] and clp[c_eq_p], clp[c_eq_x]
"""
# p_out_c = [] # not used anymore
c_eq_p, p_eq_c = np.nonzero((poly == clp[:, None]).all(-1))
if c_eq_p.size > 0:
c_eq_p = uniq_1d(c_eq_p)
p_eq_c = uniq_1d(p_eq_c)
p_eq_x, x_eq_p = np.nonzero((x_pnts == poly[:, None]).all(-1))
if p_eq_x.size > 0:
p_eq_x = uniq_1d(p_eq_x)
c_eq_x, x_eq_c = np.nonzero((x_pnts == clp[:, None]).all(-1))
if c_eq_x.size > 0:
c_eq_x = uniq_1d(c_eq_x)
if p_eq_c.size > 0 or p_eq_x.size > 0: # p_in_c + (c_eq_p, p_eq_x)
p_in_c = np.unique(np.concatenate((p_in_c, p_eq_c, p_eq_x)))
# -- (2) clp, poly point equal
if c_eq_p.size > 0 or c_eq_x.size > 0: # c_in_p + (p_eq_c, c_eq_x)
c_in_p = uniq_1d(np.concatenate((c_in_p, c_eq_p, c_eq_x)))
args = (p_in_c, c_in_p, c_eq_p, c_eq_x, p_eq_c, p_eq_x)
# cp_eq, cx_eq, px_eq) simplified output
out = [i.tolist() if isinstance(i, np.ndarray) else i for i in args]
return out
def _x_mkr_(to_chk, x_pnts, p_in_c, c_in_p): # poly, p_eq_x, c_eq_x):
"""Return intersections/crossings and checks, `xCheck`, given inputs.
to_chk : whr values, whr or whr_on
x_pnts : x_pnts or x_pnts_on
poly : input polygon
p_in_c, c_in_p : polygon points in clipper and clipper points in polygon
"""
s, r = divmod(to_chk.shape[0], 2) # check for even pairing
if r == 0:
x = (to_chk.reshape(-1, 4)).copy()
x[:, 1], x[:, 2] = x[:, 2], x[:, 1].copy()
else:
x = (to_chk[:s * 2].reshape(-1, 4)).copy()
x[:, 1], x[:, 2] = x[:, 2], x[:, 1].copy()
lastx, lasty = to_chk[s * 2:][0]
x = np.concatenate((x, np.array([[lastx, -1, lasty, -1]])), axis=0)
crossings = x.copy()
z0 = np.isin(crossings[:, :2], c_in_p)
z1 = np.isin(crossings[:, 2:4], p_in_c)
#
in_chks = np.concatenate((z0, z1), axis=1)
xCheck = np.concatenate((crossings, in_chks.astype(int)), axis=1)
# -- intersection points
x0x1 = [x_pnts[i: i + 2] for i in range(0, len(to_chk), 2)]
if x0x1[-1].shape == (1, 2):
pad = np.concatenate((x0x1[-1], np.array([[np.nan, np.nan]])), axis=0)
x0x1[-1] = pad
return xCheck, x0x1
def _f0f1_(_f0, _f1, _last, _st_en, _in, _eq, _seen):
"""Pick `from-to` points for various functions."""
if _f0 == 0:
_f0, _f1 = _last, _st_en
elif _f0 != 0 and _f0 < _f1:
_f0, _f1 = 0, _f0
tmp = _btw_chk_(_f0, _f1, _in, _seen)
return [i for i in tmp if i not in _eq]
def split_seq(seq, last, prn=False): # sec_last
"""Return a sequence of point ids split at its numeric gaps.
The defined gap is 1, since we are working with sequences of points.
Parameters
----------
seq : array_like
A sorted ndarray is expected, but will be converted if needed. Sorting
is your task.
sec_last, last : integers
Indices of the second last and last points in the sequence.
Returns
-------
A list of sub-arrays or the original sequence if there are no missing
values in the sequence.
"""
if len(seq) == 0:
return seq
if isinstance(seq, (list, tuple)):
seq = np.asarray(seq)
if seq.ndim > 1 or seq.shape[0] <= 1:
if prn:
print("\n A 1D ndarray required.")
return [seq.tolist()]
if seq[0] == 0 and last == seq[-1]:
tmp = seq[1:]
whr = np.nonzero(np.abs(tmp[1:] - tmp[:-1]) != 1)[0]
if whr.size > 0:
z = [s.tolist() for s in np.array_split(tmp, whr + 1)]
lst = z.pop()
z.insert(0, lst)
return z
else:
whr = np.nonzero(np.abs(seq[1:] - seq[:-1]) != 1)[0]
if whr.size > 0:
return [s.tolist() for s in np.array_split(seq, whr + 1)]
return [seq.tolist()]
def p_type(ply, eqX, eqOther, inOther):
"""Return point class for polygon.
Parameters
----------
ply : ndarray
Polygon points to be classed.
eqX, eqOther, inOther : ndarrays or lists of values
- poly/clipper equal to an intersection point
- one equals the other point
- one is in the other
column names (0, 1, 2 positionally)
Requires
--------
`_wn_clip_`, `nodes` are used to determine whether each point meets the
conditions outlined above.
Notes
-----
Conversion values are based on binary conversion as shown in the
`keys` line, then reclassed using a dictionary conversion.
- keys = eqX * 100 + eqOther * 10 + inOther
- 0 (0 0 0) is outside
- 1 (0 0 1) is inside with no intersection
- position before a 1 is an intersection point not at an endpoint
- 5 (1 0 1) endpoint intersects a segment
- 7 (111) clp, poly and intersection meet at a point.
Example
-------
>>> f0 = p_type(poly, p_eq_x, p_eq_c, p_in_c)
>>> f1 = p_type(clp, c_eq_x, c_eq_p, c_in_p)
"""
k = [0, 1, 10, 11, 100, 101, 110, 111]
v = [0, 1, 2, 3, 4, 5, 6, 7]
d = dict(zip(k, v))
N = ply.shape[0]
z = np.zeros((N, 5), 'int')
z[:, 0] = np.arange(N)
z[:, 1][eqX] = 1
z[:, 2][eqOther] = 1
z[:, 3][inOther] = 1
keys = z[:, 1] * 100 + z[:, 2] * 10 + z[:, 3]
vals = [d[i] for i in keys.tolist()]
z[:, 4] = vals
return z
# -- main
def clip(poly, clp):
"""Return the result of a polygon clip.
Parameters
----------
poly, clp : ndarrays representing polygons
Rings are oriented clockwise. Holes are ignored.
Requires
--------
- `_wn_clip_`, `nodes`, `_x_mkr_`
- `_concat_`, `_dist_`, `_a_eq_b_`, `_before_chk_`, `_btw_chk_`, `_to_add_`
- Fixes **** p_out_c, c_out_p not used so commented out.
# -- `run a bail_chk_` ... doesn't work b0,E because all points are in
# if nP == len(p_in_c): # but all of b0 is retained instead of the E shape
# print("...poly completely within clp...")
# return poly, None, None
# elif nC == len(c_in_p):
# print("... clp completely within poly...")
# return clp, None, None
#
Notes
-----
Sample data::
t0 = np.array([[0., 0], [0., 2], [8., 2], [8., 0.], [0., 0]])
t01 = np.array([[1., 0], [1., 2], [3., 2], [8., 2], [8., 0.], [0., 0]])
t1 = np.array([[0., 0], [0., 2], [1., 2.], [8., 2], [8., 0.], [0., 0]])
t2 = np.array([[0., 0], [0., 2], [2., 2.], [8., 2], [8., 0.], [0., 0]])
t3 = np.array([[0., 0], [0., 2], [4., 2.], [8., 2], [8., 0.], [0., 0]])
t4 = np.array([[0., 0], [0., 2], [4., 2.], [6., 2], [8., 2],
[8., 0.], [0., 0]])
s0 = np.array([[1., 1.], [2., 2.], [5., 2.], [6., 1.]])
s00 = np.array([[1., 1.], [2., 2.], [5., 2.], [6., 1.], [1., 1.]])
s01 = np.array([[1., 1.], [3., 3.], [4., 3.], [6., 1.], [1., 1.]])
s02 = np.array([[1., 1.], [3., 3.], [3.5, 1.5], [4., 3.], [6., 1.],
[1., 1.]])
s03 = np.array([[1., 1.], [2., 2], [3., 3.], [3.5, 1.5], [4., 3.],
[6., 1.], [1., 1.]]) # dupl x on line
"""
# --
if hasattr(poly, "IFT"):
poly = poly.XY
if hasattr(clp, "IFT"):
clp = clp.XY
#
# -- winding number to get points inside, on, outside each other
poly, clp = roll_arrays([poly, clp]) # roll the arrays to orient to LL
vals = _wn_clip_(poly, clp, all_info=True)
x_pnts, pInc, cInp, x_type, whr = vals # wn_
#
if len(x_pnts) == 0:
print("No intersection between `poly` and `clp`.")
# return clp, None, None
#
# -- derive the unique intersection points and get their first found index.
uni, idx, cnts = np.unique(x_pnts, return_index=True,
return_counts=True, axis=0)
idx_srt = np.sort(idx) # get the original order
x_pnts_on = x_pnts[idx_srt]
whr_on = whr[idx_srt] # whr crossings in that order
# -- run `nodes` to get the nodes, and their info
args = nodes(pInc, cInp, clp, poly, x_pnts)
p_in_c, c_in_p, c_eq_p, c_eq_x, p_eq_c, p_eq_x = args # [:6]
# cp_eq, cx_eq, px_eq = args[-3:]
#
nC = clp.shape[0]
nP = poly.shape[0]
# -- run `_x_mkr_` for both types of analysis until I choose.
xChk, x0x1 = _x_mkr_(whr_on, x_pnts_on, p_in_c, c_in_p) # poly
# difference in index pairs for clp and poly
dC_dP = np.abs(np.vstack((xChk[:, 1] - xChk[:, 0],
xChk[:, 3] - xChk[:, 2]))).T
#
c_prev, p_prev = [-1, -1]
p_seen, c_seen, tot_ = [[], [], []] # create empty lists
c_last, c_st_en = nC - 2, nC - 1 # -- clp : last, dupl. start-end pnt
p_last, p_st_en = nP - 2, nP - 1 # -- poly : last, dupl. start-end pnt
#
p_seq = split_seq(p_in_c, p_st_en) # p_last
c_seq = split_seq(c_in_p, c_st_en) # c_last
Np_seq = len(p_seq)
Nc_seq = len(c_seq)
for cnt, xC in enumerate(xChk): # xChk is the crossings list
c0_fr, c1_fr, p0_fr, p1_fr = xC[:4]
c0_to, c1_to, p0_to, p1_to = xC[:4] + 1
c0_in, c1_in, p0_in, p1_in = np.asarray(xC[-4:], bool)
in_case = "{} {} {} {}".format(*np.asarray(xC[-4:]))
x0, x1 = x0x1[cnt] # -- intersection points for the segment pair
diff_C, diff_P = dC_dP[cnt] # difference in indices
bf_c, btw_c = [], [] # Initial values for `before`, `between`
bf_p, btw_p = [], [] # for both poly and clp
c0c1_ = in_case[:3]
p0p1_ = in_case[4:]
sub_ = []
#
seq_p = [] if cnt + 1 > Np_seq else p_seq[cnt]
seq_c = [] if cnt + 1 > Nc_seq else c_seq[cnt]
# --
#
# Last segment with only one intersection point.
if c1_fr == -1: # one of x0,x1 will be (nan,nan)
p_max = max(p_seen)
c_max = max(c_seen)
btw_p = _btw_chk_(p_max, p0_fr, p_in_c, p_seen)
btw_c = _btw_chk_(c_max, c0_fr, c_in_p, c_seen)
if btw_p:
tmp = [i for i in btw_p if i in p_in_c and i not in p_eq_x]
if tmp:
p_seen.extend(tmp)
sub_.extend(poly[tmp])
if btw_c:
tmp = [i for i in btw_c if i in c_in_p and i not in c_eq_x]
if tmp:
c_seen.extend(tmp)
sub_.extend(clp[tmp])
zz = x1 if np.isnan(x0).all() else x0 # get the correct x0,x1
sub_.append(zz) # was x0, but sometimes x1 is the one! check pairs
_out_ = _to_add_(xC, sub_, p_seen, c_seen, tot_)
p_seen, c_seen, tot_ = _out_
#
# print(f"{cnt} {x0}, {x1} {xC}")
# --
break
#
# -- pre section
# -- Either `bf_c` or `bf_p` will have values, not both
if cnt == 0:
# -- before_c `bf_c`
if c0_in or diff_C == 0:
bf_c = _f0f1_(c0_fr, c1_fr, c_last, c_st_en,
c_in_p, c_eq_x, c_seen)
if bf_c:
c_seen.extend(bf_c)
if c_st_en in bf_c or c_last in bf_c:
c_seen.extend([0])
sub_.extend(clp[bf_c])
# -- before_p `bf_p`
if p0_in or diff_P == 0:
if p0_fr == 0 and p_st_en in seq_p:
bf_p = seq_p
else:
bf_p = _f0f1_(p0_fr, p1_fr, p_last, p_st_en,
p_in_c, p_eq_x, p_seen)
if bf_p:
p_seen.extend(bf_p)
if p_st_en in bf_p or p_last in bf_p:
p_seen.extend([0])
sub_.extend(poly[bf_p])
# --
# -- subsequent crossings
elif cnt > 0:
c_prev, p_prev = xChk[cnt - 1][[1, 3]] # slice previous values
if c0_fr - c_prev > 1 and c_prev not in c_eq_x:
if (c_prev in c_in_p) and (c_prev not in c_seen):
sub_.extend([clp[c_prev]])
c_seen.extend([c_prev])
bf_c = _btw_chk_(c0_fr, c_prev, c_in_p, c_seen)
# -- include other clp pnts inside
if bf_c:
sub_.extend(clp[bf_c])
c_seen.extend(bf_c)
# -- repeat for poly differences
if p0_fr - p_prev > 1 and p_prev not in p_eq_c:
if (p_prev in p_in_c) and (p_prev not in p_seen):
sub_.extend([poly[p_prev]])
p_seen.extend([p_prev])
bf_p = _btw_chk_(p0_fr, p_prev, p_in_c, p_seen)
# -- include any other poly pnts inside
if bf_p:
bf_p = [i for i in bf_p if i not in p_eq_x]
sub_.extend(poly[bf_p])
p_seen.extend(bf_p)
# --
# -- end `before`
# -- begin `between` section
if diff_C != 0:
btw_c = _btw_chk_(c0_fr, c1_fr, c_in_p, c_seen)
if btw_c and c1_fr - c0_fr == 1: # differ by 1 and both inside
c_seen.extend(btw_c)
elif btw_c and c1_fr - c0_fr == 2: # unique case!
btw_c = [i for i in btw_c if i in c_in_p] # and i in c_eq_x]
# c == x
elif btw_c:
btw_c = [i for i in btw_c if i in c_in_p and i not in c_eq_x]
c_seen.extend(btw_c)
if diff_P != 0:
btw_p = _btw_chk_(p0_fr, p1_fr, p_in_c, p_seen)
if len(btw_p) > 1: # -- skip sequences with gaps eg b4,c1
gap = btw_p[0] + len(btw_p) - 1 != btw_p[-1]
if gap:
btw_p = []
else:
p_seen.extend(btw_p) # B0,a has 1 value
# print(f"{cnt} {x0}, {x1} {xC}")
# print(f" bfc:bfp {bf_c} {bf_p}\n btc:btp {btw_c} {btw_p}")
# print(f"{cnt} {x0}, {x1} {c0c1_}{p0p1_}")
# -------- '0 0' prep --------------------------------------------
if c0c1_ == '0 0':
# -- Both pnts out but cross poly and poly pnts may be inside.
# `btw_p` may be empty or return a list of indices.
if diff_C == 0 and btw_p: # b4, c1 issue
sub_.extend(poly[btw_p]) # btw_p added to p_seen before
btw_p = None # -- empty btw_p
#
# -----------------------------------
# -- poly checks
# --
if p0p1_ == '0 0': # 0, 0, 0, 0
if p0_fr == 0: # check when p0==0 and change its value
if p1_to == p_st_en: # see if you are back at the start
sub_.extend([x1, x0])
else:
sub_.extend([x0, x1])
elif p0_fr <= p1_fr:
sub_.extend([x0, x1])
elif p0_fr > p1_fr:
sub_.extend([x1, x0])
if p1_to in p_in_c and cnt != 0:
sub_.extend([poly[p1_to]])
if c1_to in c_in_p:
sub_.extend([clp[c1_to]]) # c1,b3 add last clp
# --
elif p0p1_ == '0 1': # 0, 0, 0, 1
if p1_fr in p_eq_x:
sub_.extend([x0, x1])
p_seen.append(p1_fr)
elif p0_fr == 0 or p0_fr < p1_fr:
sub_.append(x1)
if btw_p: # -- set to [] in c0c1_ section, btw_p
sub_.extend(poly[btw_p]) # checks may be redundant
sub_.append(x0)
else:
sub_.append(x1)
if btw_p:
sub_.extend(poly[btw_p])
sub_.append(x0)
if p0_fr not in p_in_c and p0_to not in p_seen: # p0_fr not in
sub_.extend([poly[p0_to]]) # check endpoint.
p_seen.extend([p0_to])
# --
elif p0p1_ == '1 0': # 0, 0, 1, 0
if p0_fr < p1_fr:
sub_.extend([x0, x1])
else:
sub_.extend([x1, x0])
if p0_to in p_in_c and p0_to not in p_seen: # b4,c1 s03,t0
sub_.extend([poly[p0_to]])
p_seen.extend([p0_to])
if p1_to not in p_eq_x and p1_to in p_in_c: # p1 end check
sub_.extend([poly[p1_to]])
p_seen.extend([p1_to])
# --
elif p0p1_ == '1 1': # 0, 0, 1, 1
# x0,x1 on the clipping line, may be added earlier in c0c1
# alternate: all([i in p_eq_x for i in btw_p])
if btw_p is not None:
sub_.extend([x0, x1]) # or btw_p, s00,t0
#
# -------- '0 1' prep --------------------------------------------
elif c0c1_ == '0 1': # example b0,c1 b1,c0
# -- prep bf_p already added if present
if c0_fr <= c1_fr: # order additions, add first intersection
sub_.append(x0)
if btw_c:
sub_.extend(clp[btw_c])
else:
if btw_c:
sub_.extend(clp[btw_c])
sub_.append(x0)
#
# -----------------------------------
# -- poly checks
# --
if p0p1_ == '0 0': # 0, 1, 0, 0
sub_.append(x1)
# if p1_to in p_in_c: # didn't work for poly, clp
# sub_.extend([poly[p1_to]]) # from featureclass
# p_seen.extend([p1_to]) # it extrapolated
# --
elif p0p1_ == '0 1': # p1_seen: # not done
sub_.extend([poly[p1_fr]]) # 0, 1, 0, 1
# --
elif p0p1_ == '1 0': # example b0,c1 # 0, 1, 1, 0
# -- add pnts in the correct order
if p0_fr < p1_fr: # b4,c1 p1_to is > p0_fr & p1_fr
if p0_to not in btw_p and p1_to not in btw_p: # edgy1,ecl
if len(btw_p) > 0:
sub_.extend(poly[btw_p])
sub_.append(x1)
else:
sub_.append(x1)
if btw_p and p1_to not in btw_p:
sub_.extend(poly[btw_p])
if p1_to in p_in_c and p1_to not in p_seen + p_eq_x: # c2,K
if len(seq_p) == 0:
sub_.append(poly[p1_to])
elif len(seq_p) == 1:
sub_.extend([poly[p1_to]])
elif len(seq_p) > 1: # b4, c1
sub_.extend(poly[seq_p])
# to_add = [i for i in seq_p if i not in p_seen]
# if to_add:
# sub_.extend(poly[to_add])
p_seen.extend([p1_to])
# --
elif p0p1_ == '1 1': # 2 clp on same poly # 0, 1, 1, 1
if c1_fr in c_in_p and c1_fr not in c_seen:
sub_.extend([clp[c1_fr]])
sub_.append(x1)
tmp = p1_fr # -- needed for cases where p0, p1 are equal
if diff_P == 0 and p0_to in c_in_p: # diff_P == 0
tmp = p0_to
sub_.extend([poly[tmp]])
p_seen.extend([tmp])
#
# -------- '1 0' prep --------------------------------------------
elif c0c1_ == '1 0': # c0, b0 header is the reverse of '0 1'
#
if c0_fr in c_seen and cnt != 0: # check for last clipping line
if c0_fr in c_eq_x and c1_to == c_st_en:
break
# last clipping line,
if c0_fr in btw_c: # added for c1, b5
sub_.extend(clp[btw_c])
sub_.append(x0)
elif c0_fr <= c1_fr:
sub_.append(x0)
if btw_c:
sub_.extend(clp[btw_c])
elif c0_fr > c1_fr:
if btw_c:
sub_.extend(clp[btw_c])
sub_.append(x1)
#
# -----------------------------------
# -- poly checks
# --
if p0p1_ == '0 0': # 1, 0, 0, 0
if diff_P == 0: # p0_fr to p0_to (p0_to=p1_fr), eg, 2 2
sub_.append(x1) # c1, b3
elif not sub_:
sub_.append(x0)
elif btw_c: # btw_c could be 1 or more points
if c0_fr in btw_c and len(btw_c) == 1:
tmp_ = [clp[c0_fr]]
else: