forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwgeom_topo.c
5991 lines (5452 loc) · 173 KB
/
lwgeom_topo.c
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
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2015 Sandro Santilli <strk@kbt.io>
*
**********************************************************************/
#include "../postgis_config.h"
/*#define POSTGIS_DEBUG_LEVEL 1*/
#include "lwgeom_log.h"
#include "liblwgeom_internal.h"
#include "liblwgeom_topo_internal.h"
#include "lwgeom_geos.h"
#include <stdio.h>
#include <inttypes.h> /* for PRId64 */
#include <errno.h>
#include <math.h>
#ifdef WIN32
# define LWTFMT_ELEMID "lld"
#else
# define LWTFMT_ELEMID PRId64
#endif
/*********************************************************************
*
* Backend iface
*
********************************************************************/
LWT_BE_IFACE* lwt_CreateBackendIface(const LWT_BE_DATA *data)
{
LWT_BE_IFACE *iface = lwalloc(sizeof(LWT_BE_IFACE));
iface->data = data;
iface->cb = NULL;
return iface;
}
void lwt_BackendIfaceRegisterCallbacks(LWT_BE_IFACE *iface,
const LWT_BE_CALLBACKS* cb)
{
iface->cb = cb;
}
void lwt_FreeBackendIface(LWT_BE_IFACE* iface)
{
lwfree(iface);
}
/*********************************************************************
*
* Backend wrappers
*
********************************************************************/
#define CHECKCB(be, method) do { \
if ( ! (be)->cb || ! (be)->cb->method ) \
lwerror("Callback " # method " not registered by backend"); \
} while (0)
#define CB0(be, method) \
CHECKCB(be, method);\
return (be)->cb->method((be)->data)
#define CB1(be, method, a1) \
CHECKCB(be, method);\
return (be)->cb->method((be)->data, a1)
#define CBT0(to, method) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo)
#define CBT1(to, method, a1) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1)
#define CBT2(to, method, a1, a2) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1, a2)
#define CBT3(to, method, a1, a2, a3) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3)
#define CBT4(to, method, a1, a2, a3, a4) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4)
#define CBT5(to, method, a1, a2, a3, a4, a5) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5)
#define CBT6(to, method, a1, a2, a3, a4, a5, a6) \
CHECKCB((to)->be_iface, method);\
return (to)->be_iface->cb->method((to)->be_topo, a1, a2, a3, a4, a5, a6)
const char *
lwt_be_lastErrorMessage(const LWT_BE_IFACE* be)
{
CB0(be, lastErrorMessage);
}
LWT_BE_TOPOLOGY *
lwt_be_loadTopologyByName(LWT_BE_IFACE *be, const char *name)
{
CB1(be, loadTopologyByName, name);
}
static int
lwt_be_topoGetSRID(LWT_TOPOLOGY *topo)
{
CBT0(topo, topoGetSRID);
}
static double
lwt_be_topoGetPrecision(LWT_TOPOLOGY *topo)
{
CBT0(topo, topoGetPrecision);
}
static int
lwt_be_topoHasZ(LWT_TOPOLOGY *topo)
{
CBT0(topo, topoHasZ);
}
int
lwt_be_freeTopology(LWT_TOPOLOGY *topo)
{
CBT0(topo, freeTopology);
}
LWT_ISO_NODE*
lwt_be_getNodeById(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields)
{
CBT3(topo, getNodeById, ids, numelems, fields);
}
LWT_ISO_NODE*
lwt_be_getNodeWithinDistance2D(LWT_TOPOLOGY* topo, LWPOINT* pt,
double dist, int* numelems, int fields,
int limit)
{
CBT5(topo, getNodeWithinDistance2D, pt, dist, numelems, fields, limit);
}
static LWT_ISO_NODE*
lwt_be_getNodeWithinBox2D( const LWT_TOPOLOGY* topo,
const GBOX* box, int* numelems, int fields,
int limit )
{
CBT4(topo, getNodeWithinBox2D, box, numelems, fields, limit);
}
static LWT_ISO_EDGE*
lwt_be_getEdgeWithinBox2D( const LWT_TOPOLOGY* topo,
const GBOX* box, int* numelems, int fields,
int limit )
{
CBT4(topo, getEdgeWithinBox2D, box, numelems, fields, limit);
}
static LWT_ISO_FACE*
lwt_be_getFaceWithinBox2D( const LWT_TOPOLOGY* topo,
const GBOX* box, int* numelems, int fields,
int limit )
{
CBT4(topo, getFaceWithinBox2D, box, numelems, fields, limit);
}
int
lwt_be_insertNodes(LWT_TOPOLOGY* topo, LWT_ISO_NODE* node, int numelems)
{
CBT2(topo, insertNodes, node, numelems);
}
static int
lwt_be_insertFaces(LWT_TOPOLOGY* topo, LWT_ISO_FACE* face, int numelems)
{
CBT2(topo, insertFaces, face, numelems);
}
static int
lwt_be_deleteFacesById(const LWT_TOPOLOGY* topo, const LWT_ELEMID* ids, int numelems)
{
CBT2(topo, deleteFacesById, ids, numelems);
}
static int
lwt_be_deleteNodesById(const LWT_TOPOLOGY* topo, const LWT_ELEMID* ids, int numelems)
{
CBT2(topo, deleteNodesById, ids, numelems);
}
LWT_ELEMID
lwt_be_getNextEdgeId(LWT_TOPOLOGY* topo)
{
CBT0(topo, getNextEdgeId);
}
LWT_ISO_EDGE*
lwt_be_getEdgeById(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields)
{
CBT3(topo, getEdgeById, ids, numelems, fields);
}
static LWT_ISO_FACE*
lwt_be_getFaceById(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields)
{
CBT3(topo, getFaceById, ids, numelems, fields);
}
static LWT_ISO_EDGE*
lwt_be_getEdgeByNode(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields)
{
CBT3(topo, getEdgeByNode, ids, numelems, fields);
}
static LWT_ISO_EDGE*
lwt_be_getEdgeByFace(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields, const GBOX *box)
{
CBT4(topo, getEdgeByFace, ids, numelems, fields, box);
}
static LWT_ISO_NODE*
lwt_be_getNodeByFace(LWT_TOPOLOGY* topo, const LWT_ELEMID* ids,
int* numelems, int fields, const GBOX *box)
{
CBT4(topo, getNodeByFace, ids, numelems, fields, box);
}
LWT_ISO_EDGE*
lwt_be_getEdgeWithinDistance2D(LWT_TOPOLOGY* topo, LWPOINT* pt,
double dist, int* numelems, int fields,
int limit)
{
CBT5(topo, getEdgeWithinDistance2D, pt, dist, numelems, fields, limit);
}
int
lwt_be_insertEdges(LWT_TOPOLOGY* topo, LWT_ISO_EDGE* edge, int numelems)
{
CBT2(topo, insertEdges, edge, numelems);
}
int
lwt_be_updateEdges(LWT_TOPOLOGY* topo,
const LWT_ISO_EDGE* sel_edge, int sel_fields,
const LWT_ISO_EDGE* upd_edge, int upd_fields,
const LWT_ISO_EDGE* exc_edge, int exc_fields
)
{
CBT6(topo, updateEdges, sel_edge, sel_fields,
upd_edge, upd_fields,
exc_edge, exc_fields);
}
static int
lwt_be_updateNodes(LWT_TOPOLOGY* topo,
const LWT_ISO_NODE* sel_node, int sel_fields,
const LWT_ISO_NODE* upd_node, int upd_fields,
const LWT_ISO_NODE* exc_node, int exc_fields
)
{
CBT6(topo, updateNodes, sel_node, sel_fields,
upd_node, upd_fields,
exc_node, exc_fields);
}
static int
lwt_be_updateFacesById(LWT_TOPOLOGY* topo,
const LWT_ISO_FACE* faces, int numfaces
)
{
CBT2(topo, updateFacesById, faces, numfaces);
}
static int
lwt_be_updateEdgesById(LWT_TOPOLOGY* topo,
const LWT_ISO_EDGE* edges, int numedges, int upd_fields
)
{
CBT3(topo, updateEdgesById, edges, numedges, upd_fields);
}
static int
lwt_be_updateNodesById(LWT_TOPOLOGY* topo,
const LWT_ISO_NODE* nodes, int numnodes, int upd_fields
)
{
CBT3(topo, updateNodesById, nodes, numnodes, upd_fields);
}
int
lwt_be_deleteEdges(LWT_TOPOLOGY* topo,
const LWT_ISO_EDGE* sel_edge, int sel_fields
)
{
CBT2(topo, deleteEdges, sel_edge, sel_fields);
}
LWT_ELEMID
lwt_be_getFaceContainingPoint(LWT_TOPOLOGY* topo, LWPOINT* pt)
{
CBT1(topo, getFaceContainingPoint, pt);
}
int
lwt_be_updateTopoGeomEdgeSplit(LWT_TOPOLOGY* topo, LWT_ELEMID split_edge, LWT_ELEMID new_edge1, LWT_ELEMID new_edge2)
{
CBT3(topo, updateTopoGeomEdgeSplit, split_edge, new_edge1, new_edge2);
}
static int
lwt_be_updateTopoGeomFaceSplit(LWT_TOPOLOGY* topo, LWT_ELEMID split_face,
LWT_ELEMID new_face1, LWT_ELEMID new_face2)
{
CBT3(topo, updateTopoGeomFaceSplit, split_face, new_face1, new_face2);
}
static int
lwt_be_checkTopoGeomRemEdge(LWT_TOPOLOGY* topo, LWT_ELEMID edge_id,
LWT_ELEMID face_left, LWT_ELEMID face_right)
{
CBT3(topo, checkTopoGeomRemEdge, edge_id, face_left, face_right);
}
static int
lwt_be_checkTopoGeomRemNode(LWT_TOPOLOGY* topo, LWT_ELEMID node_id,
LWT_ELEMID eid1, LWT_ELEMID eid2)
{
CBT3(topo, checkTopoGeomRemNode, node_id, eid1, eid2);
}
static int
lwt_be_updateTopoGeomFaceHeal(LWT_TOPOLOGY* topo,
LWT_ELEMID face1, LWT_ELEMID face2,
LWT_ELEMID newface)
{
CBT3(topo, updateTopoGeomFaceHeal, face1, face2, newface);
}
static int
lwt_be_updateTopoGeomEdgeHeal(LWT_TOPOLOGY* topo,
LWT_ELEMID edge1, LWT_ELEMID edge2,
LWT_ELEMID newedge)
{
CBT3(topo, updateTopoGeomEdgeHeal, edge1, edge2, newedge);
}
static LWT_ELEMID*
lwt_be_getRingEdges( LWT_TOPOLOGY* topo,
LWT_ELEMID edge, int *numedges, int limit )
{
CBT3(topo, getRingEdges, edge, numedges, limit);
}
/* wrappers of backend wrappers... */
int
lwt_be_ExistsCoincidentNode(LWT_TOPOLOGY* topo, LWPOINT* pt)
{
int exists = 0;
lwt_be_getNodeWithinDistance2D(topo, pt, 0, &exists, 0, -1);
if ( exists == -1 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return 0;
}
return exists;
}
int
lwt_be_ExistsEdgeIntersectingPoint(LWT_TOPOLOGY* topo, LWPOINT* pt)
{
int exists = 0;
lwt_be_getEdgeWithinDistance2D(topo, pt, 0, &exists, 0, -1);
if ( exists == -1 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return 0;
}
return exists;
}
/************************************************************************
*
* Utility functions
*
************************************************************************/
static LWGEOM *
_lwt_toposnap(LWGEOM *src, LWGEOM *tgt, double tol)
{
LWGEOM *tmp = src;
LWGEOM *tmp2;
int changed;
int iterations = 0;
int maxiterations = lwgeom_count_vertices(tgt);
/* GEOS snapping can be unstable */
/* See https://trac.osgeo.org/geos/ticket/760 */
do {
LWGEOM *tmp3;
tmp2 = lwgeom_snap(tmp, tgt, tol);
++iterations;
changed = ( lwgeom_count_vertices(tmp2) != lwgeom_count_vertices(tmp) );
#if GEOS_NUMERIC_VERSION < 30309
/* Up to GEOS-3.3.8, snapping could duplicate points */
if ( changed ) {
tmp3 = lwgeom_remove_repeated_points( tmp2, 0 );
lwgeom_free(tmp2);
tmp2 = tmp3;
changed = ( lwgeom_count_vertices(tmp2) != lwgeom_count_vertices(tmp) );
}
#endif /* GEOS_NUMERIC_VERSION < 30309 */
LWDEBUGF(2, "After iteration %d, geometry changed ? %d (%d vs %d vertices)", iterations, changed, lwgeom_count_vertices(tmp2), lwgeom_count_vertices(tmp));
if ( tmp != src ) lwgeom_free(tmp);
tmp = tmp2;
} while ( changed && iterations <= maxiterations );
LWDEBUGF(1, "It took %d/%d iterations to properly snap",
iterations, maxiterations);
return tmp;
}
static void
_lwt_release_faces(LWT_ISO_FACE *faces, int num_faces)
{
int i;
for ( i=0; i<num_faces; ++i ) {
if ( faces[i].mbr ) lwfree(faces[i].mbr);
}
lwfree(faces);
}
static void
_lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
{
int i;
for ( i=0; i<num_edges; ++i ) {
if ( edges[i].geom ) lwline_free(edges[i].geom);
}
lwfree(edges);
}
static void
_lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
{
int i;
for ( i=0; i<num_nodes; ++i ) {
if ( nodes[i].geom ) lwpoint_free(nodes[i].geom);
}
lwfree(nodes);
}
/************************************************************************
*
* API implementation
*
************************************************************************/
LWT_TOPOLOGY *
lwt_LoadTopology( LWT_BE_IFACE *iface, const char *name )
{
LWT_BE_TOPOLOGY* be_topo;
LWT_TOPOLOGY* topo;
be_topo = lwt_be_loadTopologyByName(iface, name);
if ( ! be_topo ) {
//lwerror("Could not load topology from backend: %s",
lwerror("%s", lwt_be_lastErrorMessage(iface));
return NULL;
}
topo = lwalloc(sizeof(LWT_TOPOLOGY));
topo->be_iface = iface;
topo->be_topo = be_topo;
topo->srid = lwt_be_topoGetSRID(topo);
topo->hasZ = lwt_be_topoHasZ(topo);
topo->precision = lwt_be_topoGetPrecision(topo);
return topo;
}
void
lwt_FreeTopology( LWT_TOPOLOGY* topo )
{
if ( ! lwt_be_freeTopology(topo) ) {
lwnotice("Could not release backend topology memory: %s",
lwt_be_lastErrorMessage(topo->be_iface));
}
lwfree(topo);
}
LWT_ELEMID
lwt_AddIsoNode( LWT_TOPOLOGY* topo, LWT_ELEMID face,
LWPOINT* pt, int skipISOChecks )
{
LWT_ELEMID foundInFace = -1;
if ( ! skipISOChecks )
{
if ( lwt_be_ExistsCoincidentNode(topo, pt) ) /*x*/
{
lwerror("SQL/MM Spatial exception - coincident node");
return -1;
}
if ( lwt_be_ExistsEdgeIntersectingPoint(topo, pt) ) /*x*/
{
lwerror("SQL/MM Spatial exception - edge crosses node.");
return -1;
}
}
if ( face == -1 || ! skipISOChecks )
{
foundInFace = lwt_be_getFaceContainingPoint(topo, pt); /*x*/
if ( foundInFace == -2 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
if ( foundInFace == -1 ) foundInFace = 0;
}
if ( face == -1 ) {
face = foundInFace;
}
else if ( ! skipISOChecks && foundInFace != face ) {
#if 0
lwerror("SQL/MM Spatial exception - within face %d (not %d)",
foundInFace, face);
#else
lwerror("SQL/MM Spatial exception - not within face");
#endif
return -1;
}
LWT_ISO_NODE node;
node.node_id = -1;
node.containing_face = face;
node.geom = pt;
if ( ! lwt_be_insertNodes(topo, &node, 1) )
{
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
return node.node_id;
}
/* Check that an edge does not cross an existing node or edge
*
* @param myself the id of an edge to skip, if any
* (for ChangeEdgeGeom). Can use 0 for none.
*
* Return -1 on cross or error, 0 if everything is fine.
* Note that before returning -1, lwerror is invoked...
*/
static int
_lwt_CheckEdgeCrossing( LWT_TOPOLOGY* topo,
LWT_ELEMID start_node, LWT_ELEMID end_node,
const LWLINE *geom, LWT_ELEMID myself )
{
int i, num_nodes, num_edges;
LWT_ISO_EDGE *edges;
LWT_ISO_NODE *nodes;
const GBOX *edgebox;
GEOSGeometry *edgegg;
const GEOSPreparedGeometry* prepared_edge;
initGEOS(lwnotice, lwgeom_geos_error);
edgegg = LWGEOM2GEOS( lwline_as_lwgeom(geom), 0);
if ( ! edgegg ) {
lwerror("Could not convert edge geometry to GEOS: %s", lwgeom_geos_errmsg);
return -1;
}
prepared_edge = GEOSPrepare( edgegg );
if ( ! prepared_edge ) {
lwerror("Could not prepare edge geometry: %s", lwgeom_geos_errmsg);
return -1;
}
edgebox = lwgeom_get_bbox( lwline_as_lwgeom(geom) );
/* loop over each node within the edge's gbox */
nodes = lwt_be_getNodeWithinBox2D( topo, edgebox, &num_nodes,
LWT_COL_NODE_ALL, 0 );
LWDEBUGF(1, "lwt_be_getNodeWithinBox2D returned %d nodes", num_nodes);
if ( num_nodes == -1 ) {
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
for ( i=0; i<num_nodes; ++i )
{
LWT_ISO_NODE* node = &(nodes[i]);
GEOSGeometry *nodegg;
int contains;
if ( node->node_id == start_node ) continue;
if ( node->node_id == end_node ) continue;
/* check if the edge contains this node (not on boundary) */
nodegg = LWGEOM2GEOS( lwpoint_as_lwgeom(node->geom) , 0);
/* ST_RelateMatch(rec.relate, 'T********') */
contains = GEOSPreparedContains( prepared_edge, nodegg );
GEOSGeom_destroy(nodegg);
if (contains == 2)
{
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
_lwt_release_nodes(nodes, num_nodes);
lwerror("GEOS exception on PreparedContains: %s", lwgeom_geos_errmsg);
return -1;
}
if ( contains )
{
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
_lwt_release_nodes(nodes, num_nodes);
lwerror("SQL/MM Spatial exception - geometry crosses a node");
return -1;
}
}
if ( nodes ) _lwt_release_nodes(nodes, num_nodes);
/* may be NULL if num_nodes == 0 */
/* loop over each edge within the edge's gbox */
edges = lwt_be_getEdgeWithinBox2D( topo, edgebox, &num_edges, LWT_COL_EDGE_ALL, 0 );
LWDEBUGF(1, "lwt_be_getEdgeWithinBox2D returned %d edges", num_edges);
if ( num_edges == -1 ) {
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
for ( i=0; i<num_edges; ++i )
{
LWT_ISO_EDGE* edge = &(edges[i]);
LWT_ELEMID edge_id = edge->edge_id;
GEOSGeometry *eegg;
char *relate;
int match;
if ( edge_id == myself ) continue;
if ( ! edge->geom ) {
_lwt_release_edges(edges, num_edges);
lwerror("Edge %d has NULL geometry!", edge_id);
return -1;
}
eegg = LWGEOM2GEOS( lwline_as_lwgeom(edge->geom), 0 );
if ( ! eegg ) {
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
_lwt_release_edges(edges, num_edges);
lwerror("Could not convert edge geometry to GEOS: %s", lwgeom_geos_errmsg);
return -1;
}
LWDEBUGF(2, "Edge %d converted to GEOS", edge_id);
/* check if the edge crosses our edge (not boundary-boundary) */
relate = GEOSRelateBoundaryNodeRule(eegg, edgegg, 2);
if ( ! relate ) {
GEOSGeom_destroy(eegg);
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
_lwt_release_edges(edges, num_edges);
lwerror("GEOSRelateBoundaryNodeRule error: %s", lwgeom_geos_errmsg);
return -1;
}
LWDEBUGF(2, "Edge %d relate pattern is %s", edge_id, relate);
match = GEOSRelatePatternMatch(relate, "F********");
if ( match ) {
/* error or no interior intersection */
GEOSGeom_destroy(eegg);
GEOSFree(relate);
if ( match == 2 ) {
_lwt_release_edges(edges, num_edges);
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
lwerror("GEOSRelatePatternMatch error: %s", lwgeom_geos_errmsg);
return -1;
}
else continue; /* no interior intersection */
}
match = GEOSRelatePatternMatch(relate, "1FFF*FFF2");
if ( match ) {
_lwt_release_edges(edges, num_edges);
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
GEOSGeom_destroy(eegg);
GEOSFree(relate);
if ( match == 2 ) {
lwerror("GEOSRelatePatternMatch error: %s", lwgeom_geos_errmsg);
} else {
lwerror("SQL/MM Spatial exception - coincident edge %" LWTFMT_ELEMID,
edge_id);
}
return -1;
}
match = GEOSRelatePatternMatch(relate, "1********");
if ( match ) {
_lwt_release_edges(edges, num_edges);
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
GEOSGeom_destroy(eegg);
GEOSFree(relate);
if ( match == 2 ) {
lwerror("GEOSRelatePatternMatch error: %s", lwgeom_geos_errmsg);
} else {
lwerror("Spatial exception - geometry intersects edge %"
LWTFMT_ELEMID, edge_id);
}
return -1;
}
match = GEOSRelatePatternMatch(relate, "T********");
if ( match ) {
_lwt_release_edges(edges, num_edges);
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
GEOSGeom_destroy(eegg);
GEOSFree(relate);
if ( match == 2 ) {
lwerror("GEOSRelatePatternMatch error: %s", lwgeom_geos_errmsg);
} else {
lwerror("SQL/MM Spatial exception - geometry crosses edge %"
LWTFMT_ELEMID, edge_id);
}
return -1;
}
LWDEBUGF(2, "Edge %d analisys completed, it does no harm", edge_id);
GEOSFree(relate);
GEOSGeom_destroy(eegg);
}
if ( edges ) _lwt_release_edges(edges, num_edges);
/* would be NULL if num_edges was 0 */
GEOSPreparedGeom_destroy(prepared_edge);
GEOSGeom_destroy(edgegg);
return 0;
}
LWT_ELEMID
lwt_AddIsoEdge( LWT_TOPOLOGY* topo, LWT_ELEMID startNode,
LWT_ELEMID endNode, const LWLINE* geom )
{
int num_nodes;
int i;
LWT_ISO_EDGE newedge;
LWT_ISO_NODE *endpoints;
LWT_ELEMID containing_face = -1;
LWT_ELEMID node_ids[2];
LWT_ISO_NODE updated_nodes[2];
int skipISOChecks = 0;
POINT2D p1, p2;
/* NOT IN THE SPECS:
* A closed edge is never isolated (as it forms a face)
*/
if ( startNode == endNode )
{
lwerror("Closed edges would not be isolated, try lwt_AddEdgeNewFaces");
return -1;
}
if ( ! skipISOChecks )
{
/* Acurve must be simple */
if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
{
lwerror("SQL/MM Spatial exception - curve not simple");
return -1;
}
}
/*
* Check for:
* existence of nodes
* nodes faces match
* Extract:
* nodes face id
* nodes geoms
*/
num_nodes = 2;
node_ids[0] = startNode;
node_ids[1] = endNode;
endpoints = lwt_be_getNodeById( topo, node_ids, &num_nodes,
LWT_COL_NODE_ALL );
if ( num_nodes < 0 )
{
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
else if ( num_nodes < 2 )
{
if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
lwerror("SQL/MM Spatial exception - non-existent node");
return -1;
}
for ( i=0; i<num_nodes; ++i )
{
const LWT_ISO_NODE *n = &(endpoints[i]);
if ( n->containing_face == -1 )
{
_lwt_release_nodes(endpoints, num_nodes);
lwerror("SQL/MM Spatial exception - not isolated node");
return -1;
}
if ( containing_face == -1 ) containing_face = n->containing_face;
else if ( containing_face != n->containing_face )
{
_lwt_release_nodes(endpoints, num_nodes);
lwerror("SQL/MM Spatial exception - nodes in different faces");
return -1;
}
if ( ! skipISOChecks )
{
if ( n->node_id == startNode )
{
/* l) Check that start point of acurve match start node geoms. */
getPoint2d_p(geom->points, 0, &p1);
getPoint2d_p(n->geom->point, 0, &p2);
if ( ! p2d_same(&p1, &p2) )
{
_lwt_release_nodes(endpoints, num_nodes);
lwerror("SQL/MM Spatial exception - "
"start node not geometry start point.");
return -1;
}
}
else
{
/* m) Check that end point of acurve match end node geoms. */
getPoint2d_p(geom->points, geom->points->npoints-1, &p1);
getPoint2d_p(n->geom->point, 0, &p2);
if ( ! p2d_same(&p1, &p2) )
{
_lwt_release_nodes(endpoints, num_nodes);
lwerror("SQL/MM Spatial exception - "
"end node not geometry end point.");
return -1;
}
}
}
}
if ( num_nodes ) _lwt_release_nodes(endpoints, num_nodes);
if ( ! skipISOChecks )
{
if ( _lwt_CheckEdgeCrossing( topo, startNode, endNode, geom, 0 ) )
{
/* would have called lwerror already, leaking :( */
return -1;
}
}
/*
* All checks passed, time to prepare the new edge
*/
newedge.edge_id = lwt_be_getNextEdgeId( topo );
if ( newedge.edge_id == -1 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
/* TODO: this should likely be an exception instead ! */
if ( containing_face == -1 ) containing_face = 0;
newedge.start_node = startNode;
newedge.end_node = endNode;
newedge.face_left = newedge.face_right = containing_face;
newedge.next_left = -newedge.edge_id;
newedge.next_right = newedge.edge_id;
newedge.geom = (LWLINE *)geom; /* const cast.. */
int ret = lwt_be_insertEdges(topo, &newedge, 1);
if ( ret == -1 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
} else if ( ret == 0 ) {
lwerror("Insertion of split edge failed (no reason)");
return -1;
}
/*
* Update Node containing_face values
*
* the nodes anode and anothernode are no more isolated
* because now there is an edge connecting them
*/
updated_nodes[0].node_id = startNode;
updated_nodes[0].containing_face = -1;
updated_nodes[1].node_id = endNode;
updated_nodes[1].containing_face = -1;
ret = lwt_be_updateNodesById(topo, updated_nodes, 2,
LWT_COL_NODE_CONTAINING_FACE);
if ( ret == -1 ) {
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return -1;
}
return newedge.edge_id;
}
static LWCOLLECTION *
_lwt_EdgeSplit( LWT_TOPOLOGY* topo, LWT_ELEMID edge, LWPOINT* pt, int skipISOChecks, LWT_ISO_EDGE** oldedge )
{
LWGEOM *split;
LWCOLLECTION *split_col;
int i;
/* Get edge */
i = 1;
LWDEBUG(1, "calling lwt_be_getEdgeById");
*oldedge = lwt_be_getEdgeById(topo, &edge, &i, LWT_COL_EDGE_ALL);
LWDEBUGF(1, "lwt_be_getEdgeById returned %p", *oldedge);
if ( ! *oldedge )
{
LWDEBUGF(1, "lwt_be_getEdgeById returned NULL and set i=%d", i);
if ( i == -1 )
{
lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
return NULL;
}
else if ( i == 0 )
{
lwerror("SQL/MM Spatial exception - non-existent edge");
return NULL;
}
else
{
lwerror("Backend coding error: getEdgeById callback returned NULL "
"but numelements output parameter has value %d "
"(expected 0 or 1)", i);
return NULL;
}
}
/*
* - check if a coincident node already exists
*/
if ( ! skipISOChecks )
{
LWDEBUG(1, "calling lwt_be_ExistsCoincidentNode");
if ( lwt_be_ExistsCoincidentNode(topo, pt) ) /*x*/
{
LWDEBUG(1, "lwt_be_ExistsCoincidentNode returned");
_lwt_release_edges(*oldedge, 1);
lwerror("SQL/MM Spatial exception - coincident node");
return NULL;
}
LWDEBUG(1, "lwt_be_ExistsCoincidentNode returned");
}
/* Split edge */