PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lwt_ChangeEdgeGeom()

int lwt_ChangeEdgeGeom ( LWT_TOPOLOGY topo,
LWT_ELEMID  edge,
LWLINE curve 
)

Changes the shape of an edge without affecting the topology structure.

For ST_ChangeEdgeGeom

Parameters
topothe topology to operate on
curvethe edge geometry
Returns
0 on success, -1 on error (liblwgeom error handler will be invoked with error message)

Definition at line 3179 of file lwgeom_topo.c.

3180 {
3181  LWT_ISO_EDGE *oldedge;
3182  LWT_ISO_EDGE newedge;
3183  POINT2D p1, p2, pt;
3184  int i;
3185  int isclosed = 0;
3186 
3187  /* curve must be simple */
3188  if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
3189  {
3190  lwerror("SQL/MM Spatial exception - curve not simple");
3191  return -1;
3192  }
3193 
3194  i = 1;
3195  oldedge = lwt_be_getEdgeById(topo, &edge_id, &i, LWT_COL_EDGE_ALL);
3196  if ( ! oldedge )
3197  {
3198  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3199  "lwt_be_getEdgeById returned NULL and set i=%d", i);
3200  if ( i == -1 )
3201  {
3202  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3203  return -1;
3204  }
3205  else if ( i == 0 )
3206  {
3207  lwerror("SQL/MM Spatial exception - non-existent edge %"
3208  LWTFMT_ELEMID, edge_id);
3209  return -1;
3210  }
3211  else
3212  {
3213  lwerror("Backend coding error: getEdgeById callback returned NULL "
3214  "but numelements output parameter has value %d "
3215  "(expected 0 or 1)", i);
3216  return -1;
3217  }
3218  }
3219 
3220  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3221  "old edge has %d points, new edge has %d points",
3222  oldedge->geom->points->npoints, geom->points->npoints);
3223 
3224  /*
3225  * e) Check StartPoint consistency
3226  */
3227  getPoint2d_p(oldedge->geom->points, 0, &p1);
3228  getPoint2d_p(geom->points, 0, &pt);
3229  if ( ! p2d_same(&p1, &pt) )
3230  {
3231  _lwt_release_edges(oldedge, 1);
3232  lwerror("SQL/MM Spatial exception - "
3233  "start node not geometry start point.");
3234  return -1;
3235  }
3236 
3237  /*
3238  * f) Check EndPoint consistency
3239  */
3240  if ( oldedge->geom->points->npoints < 2 )
3241  {
3242  _lwt_release_edges(oldedge, 1);
3243  lwerror("Corrupted topology: edge %" LWTFMT_ELEMID
3244  " has less than 2 vertices", oldedge->edge_id);
3245  return -1;
3246  }
3247  getPoint2d_p(oldedge->geom->points, oldedge->geom->points->npoints-1, &p2);
3248  if ( geom->points->npoints < 2 )
3249  {
3250  _lwt_release_edges(oldedge, 1);
3251  lwerror("Invalid edge: less than 2 vertices");
3252  return -1;
3253  }
3254  getPoint2d_p(geom->points, geom->points->npoints-1, &pt);
3255  if ( ! p2d_same(&pt, &p2) )
3256  {
3257  _lwt_release_edges(oldedge, 1);
3258  lwerror("SQL/MM Spatial exception - "
3259  "end node not geometry end point.");
3260  return -1;
3261  }
3262 
3263  /* Not in the specs:
3264  * if the edge is closed, check we didn't change winding !
3265  * (should be part of isomorphism checking)
3266  */
3267  if ( oldedge->start_node == oldedge->end_node )
3268  {
3269  isclosed = 1;
3270 #if 1 /* TODO: this is actually bogus as a test */
3271  /* check for valid edge (distinct vertices must exist) */
3272  if ( ! _lwt_GetInteriorEdgePoint(geom, &pt) )
3273  {
3274  _lwt_release_edges(oldedge, 1);
3275  lwerror("Invalid edge (no two distinct vertices exist)");
3276  return -1;
3277  }
3278 #endif
3279 
3280  if ( ptarray_isccw(oldedge->geom->points) !=
3281  ptarray_isccw(geom->points) )
3282  {
3283  _lwt_release_edges(oldedge, 1);
3284  lwerror("Edge twist at node POINT(%g %g)", p1.x, p1.y);
3285  return -1;
3286  }
3287  }
3288 
3289  if ( _lwt_CheckEdgeCrossing(topo, oldedge->start_node,
3290  oldedge->end_node, geom, edge_id ) )
3291  {
3292  /* would have called lwerror already, leaking :( */
3293  _lwt_release_edges(oldedge, 1);
3294  return -1;
3295  }
3296 
3297  LWDEBUG(1, "lwt_ChangeEdgeGeom: "
3298  "edge crossing check passed ");
3299 
3300  /*
3301  * Not in the specs:
3302  * Check topological isomorphism
3303  */
3304 
3305  /* Check that the "motion range" doesn't include any node */
3306  // 1. compute combined bbox of old and new edge
3307  GBOX mbox; /* motion box */
3308  lwgeom_add_bbox((LWGEOM*)oldedge->geom); /* just in case */
3309  lwgeom_add_bbox((LWGEOM*)geom); /* just in case */
3310  gbox_union(oldedge->geom->bbox, geom->bbox, &mbox);
3311  // 2. fetch all nodes in the combined box
3312  LWT_ISO_NODE *nodes;
3313  int numnodes;
3314  nodes = lwt_be_getNodeWithinBox2D(topo, &mbox, &numnodes,
3315  LWT_COL_NODE_ALL, 0);
3316  LWDEBUGF(1, "lwt_be_getNodeWithinBox2D returned %d nodes", numnodes);
3317  if ( numnodes == -1 ) {
3318  _lwt_release_edges(oldedge, 1);
3319  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3320  return -1;
3321  }
3322  // 3. if any node beside endnodes are found:
3323  if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3324  {{
3325  // 3.2. bail out if any node is in one and not the other
3326  for (i=0; i<numnodes; ++i)
3327  {
3328  LWT_ISO_NODE *n = &(nodes[i]);
3329  if ( n->node_id == oldedge->start_node ) continue;
3330  if ( n->node_id == oldedge->end_node ) continue;
3331  const POINT2D *pt = getPoint2d_cp(n->geom->point, 0);
3332  int ocont = ptarray_contains_point_partial(oldedge->geom->points, pt, isclosed, NULL) == LW_INSIDE;
3333  int ncont = ptarray_contains_point_partial(geom->points, pt, isclosed, NULL) == LW_INSIDE;
3334  if (ocont != ncont)
3335  {
3336  size_t sz;
3337  char *wkt = lwgeom_to_wkt(lwpoint_as_lwgeom(n->geom), WKT_ISO, 15, &sz);
3338  _lwt_release_nodes(nodes, numnodes);
3339  lwerror("Edge motion collision at %s", wkt);
3340  lwfree(wkt); /* would not necessarely reach this point */
3341  return -1;
3342  }
3343  }
3344  }}
3345  if ( numnodes ) _lwt_release_nodes(nodes, numnodes);
3346 
3347  LWDEBUG(1, "nodes containment check passed");
3348 
3349  /*
3350  * Check edge adjacency before
3351  * TODO: can be optimized to gather azimuths of all edge ends once
3352  */
3353 
3354  edgeend span_pre, epan_pre;
3355  /* initialize span_pre.myaz and epan_pre.myaz with existing edge */
3356  i = _lwt_InitEdgeEndByLine(&span_pre, &epan_pre,
3357  oldedge->geom, &p1, &p2);
3358  if ( i ) return -1; /* lwerror should have been raised */
3359  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_pre,
3360  isclosed ? &epan_pre : NULL, edge_id );
3361  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_pre,
3362  isclosed ? &span_pre : NULL, edge_id );
3363 
3364  LWDEBUGF(1, "edges adjacent to old edge are %" LWTFMT_ELEMID
3365  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3366  " and %" LWTFMT_ELEMID " (last point)",
3367  span_pre.nextCW, span_pre.nextCCW,
3368  epan_pre.nextCW, epan_pre.nextCCW);
3369 
3370  /* update edge geometry */
3371  newedge.edge_id = edge_id;
3372  newedge.geom = geom;
3373  i = lwt_be_updateEdgesById(topo, &newedge, 1, LWT_COL_EDGE_GEOM);
3374  if ( i == -1 )
3375  {
3376  _lwt_release_edges(oldedge, 1);
3377  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3378  return -1;
3379  }
3380  if ( ! i )
3381  {
3382  _lwt_release_edges(oldedge, 1);
3383  lwerror("Unexpected error: %d edges updated when expecting 1", i);
3384  return -1;
3385  }
3386 
3387  /*
3388  * Check edge adjacency after
3389  */
3390  edgeend span_post, epan_post;
3391  i = _lwt_InitEdgeEndByLine(&span_post, &epan_post, geom, &p1, &p2);
3392  if ( i ) return -1; /* lwerror should have been raised */
3393  /* initialize epan_post.myaz and epan_post.myaz */
3394  i = _lwt_InitEdgeEndByLine(&span_post, &epan_post,
3395  geom, &p1, &p2);
3396  if ( i ) return -1; /* lwerror should have been raised */
3397  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_post,
3398  isclosed ? &epan_post : NULL, edge_id );
3399  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_post,
3400  isclosed ? &span_post : NULL, edge_id );
3401 
3402  LWDEBUGF(1, "edges adjacent to new edge are %" LWTFMT_ELEMID
3403  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3404  " and %" LWTFMT_ELEMID " (last point)",
3405  span_pre.nextCW, span_pre.nextCCW,
3406  epan_pre.nextCW, epan_pre.nextCCW);
3407 
3408 
3409  /* Bail out if next CW or CCW edge on start node changed */
3410  if ( span_pre.nextCW != span_post.nextCW ||
3411  span_pre.nextCCW != span_post.nextCCW )
3412  {{
3413  LWT_ELEMID nid = oldedge->start_node;
3414  _lwt_release_edges(oldedge, 1);
3415  lwerror("Edge changed disposition around start node %"
3416  LWTFMT_ELEMID, nid);
3417  return -1;
3418  }}
3419 
3420  /* Bail out if next CW or CCW edge on end node changed */
3421  if ( epan_pre.nextCW != epan_post.nextCW ||
3422  epan_pre.nextCCW != epan_post.nextCCW )
3423  {{
3424  LWT_ELEMID nid = oldedge->end_node;
3425  _lwt_release_edges(oldedge, 1);
3426  lwerror("Edge changed disposition around end node %"
3427  LWTFMT_ELEMID, nid);
3428  return -1;
3429  }}
3430 
3431  /*
3432  -- Update faces MBR of left and right faces
3433  -- TODO: think about ways to optimize this part, like see if
3434  -- the old edge geometry participated in the definition
3435  -- of the current MBR (for shrinking) or the new edge MBR
3436  -- would be larger than the old face MBR...
3437  --
3438  */
3439  const GBOX* oldbox = lwgeom_get_bbox(lwline_as_lwgeom(oldedge->geom));
3440  const GBOX* newbox = lwgeom_get_bbox(lwline_as_lwgeom(geom));
3441  if ( ! gbox_same(oldbox, newbox) )
3442  {{
3443  int facestoupdate = 0;
3444  LWT_ISO_FACE faces[2];
3445  LWGEOM *nface1 = NULL;
3446  LWGEOM *nface2 = NULL;
3447  if ( oldedge->face_left > 0 )
3448  {
3449  nface1 = lwt_GetFaceGeometry(topo, oldedge->face_left);
3450  if ( ! nface1 )
3451  {
3452  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3453  LWTFMT_ELEMID ", on the left of edge %" LWTFMT_ELEMID,
3454  oldedge->face_left, edge_id);
3455  return -1;
3456  }
3457  #if 0
3458  {
3459  size_t sz;
3460  char *wkt = lwgeom_to_wkt(nface1, WKT_EXTENDED, 2, &sz);
3461  LWDEBUGF(1, "new geometry of face left (%d): %s", (int)oldedge->face_left, wkt);
3462  lwfree(wkt);
3463  }
3464  #endif
3465  lwgeom_add_bbox(nface1);
3466  if ( ! nface1->bbox )
3467  {
3468  lwerror("Corrupted topology: face %d, left of edge %d, has no bbox",
3469  oldedge->face_left, edge_id);
3470  return -1;
3471  }
3472  faces[facestoupdate].face_id = oldedge->face_left;
3473  /* ownership left to nface */
3474  faces[facestoupdate++].mbr = nface1->bbox;
3475  }
3476  if ( oldedge->face_right > 0
3477  /* no need to update twice the same face.. */
3478  && oldedge->face_right != oldedge->face_left )
3479  {
3480  nface2 = lwt_GetFaceGeometry(topo, oldedge->face_right);
3481  if ( ! nface2 )
3482  {
3483  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3484  LWTFMT_ELEMID ", on the right of edge %" LWTFMT_ELEMID,
3485  oldedge->face_right, edge_id);
3486  return -1;
3487  }
3488  #if 0
3489  {
3490  size_t sz;
3491  char *wkt = lwgeom_to_wkt(nface2, WKT_EXTENDED, 2, &sz);
3492  LWDEBUGF(1, "new geometry of face right (%d): %s", (int)oldedge->face_right, wkt);
3493  lwfree(wkt);
3494  }
3495  #endif
3496  lwgeom_add_bbox(nface2);
3497  if ( ! nface2->bbox )
3498  {
3499  lwerror("Corrupted topology: face %d, right of edge %d, has no bbox",
3500  oldedge->face_right, edge_id);
3501  return -1;
3502  }
3503  faces[facestoupdate].face_id = oldedge->face_right;
3504  faces[facestoupdate++].mbr = nface2->bbox; /* ownership left to nface */
3505  }
3506  LWDEBUGF(1, "%d faces to update", facestoupdate);
3507  if ( facestoupdate )
3508  {
3509  i = lwt_be_updateFacesById( topo, &(faces[0]), facestoupdate );
3510  if ( i != facestoupdate )
3511  {
3512  if ( nface1 ) lwgeom_free(nface1);
3513  if ( nface2 ) lwgeom_free(nface2);
3514  _lwt_release_edges(oldedge, 1);
3515  if ( i == -1 )
3516  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3517  else
3518  lwerror("Unexpected error: %d faces found when expecting 1", i);
3519  return -1;
3520  }
3521  }
3522  if ( nface1 ) lwgeom_free(nface1);
3523  if ( nface2 ) lwgeom_free(nface2);
3524  }} else {{
3525  lwnotice("BBOX of changed edge did not change");
3526  }}
3527 
3528  LWDEBUG(1, "all done, cleaning up edges");
3529 
3530  _lwt_release_edges(oldedge, 1);
3531  return 0; /* success */
3532 }
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: g_box.c:171
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
Definition: g_box.c:142
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define WKT_EXTENDED
Definition: liblwgeom.h:2077
int lwgeom_is_simple(const LWGEOM *lwgeom)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:348
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
#define WKT_ISO
Definition: liblwgeom.h:2075
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:676
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:734
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:686
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwgeom_api.c:374
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:740
#define LW_INSIDE
Constants for point-in-polygon return values.
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1028
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_ALL
#define LWT_COL_EDGE_GEOM
#define LWT_COL_NODE_ALL
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
LWT_ISO_EDGE * lwt_be_getEdgeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, int *numelems, int fields)
Definition: lwgeom_topo.c:225
static int _lwt_InitEdgeEndByLine(edgeend *fee, edgeend *lee, LWLINE *edge, POINT2D *fp, POINT2D *lp)
Definition: lwgeom_topo.c:1434
static int _lwt_GetInteriorEdgePoint(const LWLINE *edge, POINT2D *ip)
Definition: lwgeom_topo.c:1710
LWGEOM * lwt_GetFaceGeometry(LWT_TOPOLOGY *topo, LWT_ELEMID faceid)
Return the geometry of a face.
Definition: lwgeom_topo.c:2786
static int lwt_be_updateFacesById(LWT_TOPOLOGY *topo, const LWT_ISO_FACE *faces, int numfaces)
Definition: lwgeom_topo.c:298
static int _lwt_CheckEdgeCrossing(LWT_TOPOLOGY *topo, LWT_ELEMID start_node, LWT_ELEMID end_node, const LWLINE *geom, LWT_ELEMID myself)
Definition: lwgeom_topo.c:601
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:467
static LWT_ISO_NODE * lwt_be_getNodeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, int *numelems, int fields, int limit)
Definition: lwgeom_topo.c:171
static int _lwt_FindAdjacentEdges(LWT_TOPOLOGY *topo, LWT_ELEMID node, edgeend *data, edgeend *other, int myedge_id)
Definition: lwgeom_topo.c:1492
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:457
static int lwt_be_updateEdgesById(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *edges, int numedges, int upd_fields)
Definition: lwgeom_topo.c:306
GBOX * bbox
Definition: liblwgeom.h:401
GBOX * bbox
Definition: liblwgeom.h:423
POINTARRAY * points
Definition: liblwgeom.h:425
POINTARRAY * point
Definition: liblwgeom.h:414
LWT_ELEMID face_right
LWT_ELEMID end_node
LWT_ELEMID face_left
LWLINE * geom
LWT_ELEMID edge_id
LWT_ELEMID start_node
LWT_ELEMID face_id
LWT_ELEMID node_id
LWPOINT * geom
const LWT_BE_IFACE * be_iface
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331
uint32_t npoints
Definition: liblwgeom.h:374
LWT_ELEMID nextCCW
Definition: lwgeom_topo.c:1378
LWT_ELEMID nextCW
Definition: lwgeom_topo.c:1374

References _lwt_CheckEdgeCrossing(), _lwt_FindAdjacentEdges(), _lwt_GetInteriorEdgePoint(), _lwt_InitEdgeEndByLine(), _lwt_release_edges(), _lwt_release_nodes(), LWGEOM::bbox, LWLINE::bbox, LWT_TOPOLOGY_T::be_iface, LWT_ISO_EDGE::edge_id, LWT_ISO_EDGE::end_node, LWT_ISO_FACE::face_id, LWT_ISO_EDGE::face_left, LWT_ISO_EDGE::face_right, gbox_same(), gbox_union(), LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, getPoint2d_cp(), getPoint2d_p(), LW_INSIDE, LWDEBUG, LWDEBUGF, lwerror(), lwfree(), lwgeom_add_bbox(), lwgeom_free(), lwgeom_get_bbox(), lwgeom_is_simple(), lwgeom_to_wkt(), lwline_as_lwgeom(), lwnotice(), lwpoint_as_lwgeom(), lwt_be_getEdgeById(), lwt_be_getNodeWithinBox2D(), lwt_be_lastErrorMessage(), lwt_be_updateEdgesById(), lwt_be_updateFacesById(), LWT_COL_EDGE_ALL, LWT_COL_EDGE_GEOM, LWT_COL_NODE_ALL, lwt_GetFaceGeometry(), LWTFMT_ELEMID, LWT_ISO_FACE::mbr, edgeend_t::nextCCW, edgeend_t::nextCW, LWT_ISO_NODE::node_id, POINTARRAY::npoints, p2d_same(), LWPOINT::point, LWLINE::points, ptarray_contains_point_partial(), ptarray_isccw(), LWT_ISO_EDGE::start_node, WKT_EXTENDED, WKT_ISO, POINT2D::x, and POINT2D::y.

Referenced by _lwt_AddPoint().

Here is the call graph for this function:
Here is the caller graph for this function: