PostGIS  3.2.2dev-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 3258 of file lwgeom_topo.c.

3259 {
3260  LWT_ISO_EDGE *oldedge;
3261  LWT_ISO_EDGE newedge;
3262  POINT2D p1, p2, pt;
3263  uint64_t i;
3264  int isclosed = 0;
3265 
3266  /* curve must be simple */
3267  if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
3268  {
3269  lwerror("SQL/MM Spatial exception - curve not simple");
3270  return -1;
3271  }
3272 
3273  i = 1;
3274  oldedge = lwt_be_getEdgeById(topo, &edge_id, &i, LWT_COL_EDGE_ALL);
3275  if ( ! oldedge )
3276  {
3277  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3278  "lwt_be_getEdgeById returned NULL and set i=%d", i);
3279  if (i == UINT64_MAX)
3280  {
3281  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3282  return -1;
3283  }
3284  else if ( i == 0 )
3285  {
3286  lwerror("SQL/MM Spatial exception - non-existent edge %"
3287  LWTFMT_ELEMID, edge_id);
3288  return -1;
3289  }
3290  else
3291  {
3292  lwerror("Backend coding error: getEdgeById callback returned NULL "
3293  "but numelements output parameter has value %d "
3294  "(expected 0 or 1)", i);
3295  return -1;
3296  }
3297  }
3298 
3299  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3300  "old edge has %d points, new edge has %d points",
3301  oldedge->geom->points->npoints, geom->points->npoints);
3302 
3303  /*
3304  * e) Check StartPoint consistency
3305  */
3306  getPoint2d_p(oldedge->geom->points, 0, &p1);
3307  getPoint2d_p(geom->points, 0, &pt);
3308  if ( ! p2d_same(&p1, &pt) )
3309  {
3310  _lwt_release_edges(oldedge, 1);
3311  lwerror("SQL/MM Spatial exception - "
3312  "start node not geometry start point.");
3313  return -1;
3314  }
3315 
3316  /*
3317  * f) Check EndPoint consistency
3318  */
3319  if ( oldedge->geom->points->npoints < 2 )
3320  {
3321  _lwt_release_edges(oldedge, 1);
3322  lwerror("Corrupted topology: edge %" LWTFMT_ELEMID
3323  " has less than 2 vertices", oldedge->edge_id);
3324  return -1;
3325  }
3326  getPoint2d_p(oldedge->geom->points, oldedge->geom->points->npoints-1, &p2);
3327  if ( geom->points->npoints < 2 )
3328  {
3329  _lwt_release_edges(oldedge, 1);
3330  lwerror("Invalid edge: less than 2 vertices");
3331  return -1;
3332  }
3333  getPoint2d_p(geom->points, geom->points->npoints-1, &pt);
3334  if ( ! p2d_same(&pt, &p2) )
3335  {
3336  _lwt_release_edges(oldedge, 1);
3337  lwerror("SQL/MM Spatial exception - "
3338  "end node not geometry end point.");
3339  return -1;
3340  }
3341 
3342  /* Not in the specs:
3343  * if the edge is closed, check we didn't change winding !
3344  * (should be part of isomorphism checking)
3345  */
3346  if ( oldedge->start_node == oldedge->end_node )
3347  {
3348  isclosed = 1;
3349 #if 1 /* TODO: this is actually bogus as a test */
3350  /* check for valid edge (distinct vertices must exist) */
3351  if ( ! _lwt_GetInteriorEdgePoint(geom, &pt) )
3352  {
3353  _lwt_release_edges(oldedge, 1);
3354  lwerror("Invalid edge (no two distinct vertices exist)");
3355  return -1;
3356  }
3357 #endif
3358 
3359  if ( ptarray_isccw(oldedge->geom->points) !=
3360  ptarray_isccw(geom->points) )
3361  {
3362  _lwt_release_edges(oldedge, 1);
3363  lwerror("Edge twist at node POINT(%g %g)", p1.x, p1.y);
3364  return -1;
3365  }
3366  }
3367 
3368  if ( _lwt_CheckEdgeCrossing(topo, oldedge->start_node,
3369  oldedge->end_node, geom, edge_id ) )
3370  {
3371  /* would have called lwerror already, leaking :( */
3372  _lwt_release_edges(oldedge, 1);
3373  return -1;
3374  }
3375 
3376  LWDEBUG(1, "lwt_ChangeEdgeGeom: "
3377  "edge crossing check passed ");
3378 
3379  /*
3380  * Not in the specs:
3381  * Check topological isomorphism
3382  */
3383 
3384  /* Check that the "motion range" doesn't include any node */
3385  // 1. compute combined bbox of old and new edge
3386  GBOX mbox; /* motion box */
3387  lwgeom_add_bbox((LWGEOM*)oldedge->geom); /* just in case */
3388  lwgeom_add_bbox((LWGEOM*)geom); /* just in case */
3389  gbox_union(oldedge->geom->bbox, geom->bbox, &mbox);
3390  // 2. fetch all nodes in the combined box
3391  LWT_ISO_NODE *nodes;
3392  uint64_t numnodes;
3393  nodes = lwt_be_getNodeWithinBox2D(topo, &mbox, &numnodes,
3394  LWT_COL_NODE_ALL, 0);
3395  LWDEBUGF(1, "lwt_be_getNodeWithinBox2D returned %d nodes", numnodes);
3396  if (numnodes == UINT64_MAX)
3397  {
3398  _lwt_release_edges(oldedge, 1);
3399  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3400  return -1;
3401  }
3402  // 3. if any node beside endnodes are found:
3403  if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3404  {{
3405  // 3.2. bail out if any node is in one and not the other
3406  for (i=0; i<numnodes; ++i)
3407  {
3408  LWT_ISO_NODE *n = &(nodes[i]);
3409  if ( n->node_id == oldedge->start_node ) continue;
3410  if ( n->node_id == oldedge->end_node ) continue;
3411  const POINT2D *pt = getPoint2d_cp(n->geom->point, 0);
3412  int ocont = ptarray_contains_point_partial(oldedge->geom->points, pt, isclosed, NULL) == LW_INSIDE;
3413  int ncont = ptarray_contains_point_partial(geom->points, pt, isclosed, NULL) == LW_INSIDE;
3414  if (ocont != ncont)
3415  {
3416  size_t sz;
3417  char *wkt = lwgeom_to_wkt(lwpoint_as_lwgeom(n->geom), WKT_ISO, 15, &sz);
3418  _lwt_release_nodes(nodes, numnodes);
3419  lwerror("Edge motion collision at %s", wkt);
3420  lwfree(wkt); /* would not necessarely reach this point */
3421  return -1;
3422  }
3423  }
3424  }}
3425  if ( numnodes ) _lwt_release_nodes(nodes, numnodes);
3426 
3427  LWDEBUG(1, "nodes containment check passed");
3428 
3429  /*
3430  * Check edge adjacency before
3431  * TODO: can be optimized to gather azimuths of all edge ends once
3432  */
3433 
3434  edgeend span_pre, epan_pre;
3435  /* initialize span_pre.myaz and epan_pre.myaz with existing edge */
3436  int res = _lwt_InitEdgeEndByLine(&span_pre, &epan_pre, oldedge->geom, &p1, &p2);
3437  if (res)
3438  return -1; /* lwerror should have been raised */
3439  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_pre,
3440  isclosed ? &epan_pre : NULL, edge_id );
3441  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_pre,
3442  isclosed ? &span_pre : NULL, edge_id );
3443 
3444  LWDEBUGF(1, "edges adjacent to old edge are %" LWTFMT_ELEMID
3445  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3446  " and %" LWTFMT_ELEMID " (last point)",
3447  span_pre.nextCW, span_pre.nextCCW,
3448  epan_pre.nextCW, epan_pre.nextCCW);
3449 
3450  /* update edge geometry */
3451  newedge.edge_id = edge_id;
3452  newedge.geom = geom;
3453  res = lwt_be_updateEdgesById(topo, &newedge, 1, LWT_COL_EDGE_GEOM);
3454  if (res == -1)
3455  {
3456  _lwt_release_edges(oldedge, 1);
3457  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3458  return -1;
3459  }
3460  if (!res)
3461  {
3462  _lwt_release_edges(oldedge, 1);
3463  lwerror("Unexpected error: %d edges updated when expecting 1", i);
3464  return -1;
3465  }
3466 
3467  /*
3468  * Check edge adjacency after
3469  */
3470  edgeend span_post, epan_post;
3471  /* initialize epan_post.myaz and epan_post.myaz */
3472  res = _lwt_InitEdgeEndByLine(&span_post, &epan_post, geom, &p1, &p2);
3473  if (res)
3474  return -1; /* lwerror should have been raised */
3475  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_post,
3476  isclosed ? &epan_post : NULL, edge_id );
3477  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_post,
3478  isclosed ? &span_post : NULL, edge_id );
3479 
3480  LWDEBUGF(1, "edges adjacent to new edge are %" LWTFMT_ELEMID
3481  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3482  " and %" LWTFMT_ELEMID " (last point)",
3483  span_pre.nextCW, span_pre.nextCCW,
3484  epan_pre.nextCW, epan_pre.nextCCW);
3485 
3486 
3487  /* Bail out if next CW or CCW edge on start node changed */
3488  if ( span_pre.nextCW != span_post.nextCW ||
3489  span_pre.nextCCW != span_post.nextCCW )
3490  {{
3491  LWT_ELEMID nid = oldedge->start_node;
3492  _lwt_release_edges(oldedge, 1);
3493  lwerror("Edge changed disposition around start node %"
3494  LWTFMT_ELEMID, nid);
3495  return -1;
3496  }}
3497 
3498  /* Bail out if next CW or CCW edge on end node changed */
3499  if ( epan_pre.nextCW != epan_post.nextCW ||
3500  epan_pre.nextCCW != epan_post.nextCCW )
3501  {{
3502  LWT_ELEMID nid = oldedge->end_node;
3503  _lwt_release_edges(oldedge, 1);
3504  lwerror("Edge changed disposition around end node %"
3505  LWTFMT_ELEMID, nid);
3506  return -1;
3507  }}
3508 
3509  /*
3510  -- Update faces MBR of left and right faces
3511  -- TODO: think about ways to optimize this part, like see if
3512  -- the old edge geometry participated in the definition
3513  -- of the current MBR (for shrinking) or the new edge MBR
3514  -- would be larger than the old face MBR...
3515  --
3516  */
3517  const GBOX* oldbox = lwgeom_get_bbox(lwline_as_lwgeom(oldedge->geom));
3518  const GBOX* newbox = lwgeom_get_bbox(lwline_as_lwgeom(geom));
3519  if ( ! gbox_same(oldbox, newbox) )
3520  {
3521  uint64_t facestoupdate = 0;
3522  LWT_ISO_FACE faces[2];
3523  LWGEOM *nface1 = NULL;
3524  LWGEOM *nface2 = NULL;
3525  if ( oldedge->face_left > 0 )
3526  {
3527  nface1 = lwt_GetFaceGeometry(topo, oldedge->face_left);
3528  if ( ! nface1 )
3529  {
3530  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3531  LWTFMT_ELEMID ", on the left of edge %" LWTFMT_ELEMID,
3532  oldedge->face_left, edge_id);
3533  return -1;
3534  }
3535  #if 0
3536  {
3537  size_t sz;
3538  char *wkt = lwgeom_to_wkt(nface1, WKT_EXTENDED, 2, &sz);
3539  LWDEBUGF(1, "new geometry of face left (%d): %s", (int)oldedge->face_left, wkt);
3540  lwfree(wkt);
3541  }
3542  #endif
3543  lwgeom_add_bbox(nface1);
3544  if ( ! nface1->bbox )
3545  {
3546  lwerror("Corrupted topology: face %d, left of edge %d, has no bbox",
3547  oldedge->face_left, edge_id);
3548  return -1;
3549  }
3550  faces[facestoupdate].face_id = oldedge->face_left;
3551  /* ownership left to nface */
3552  faces[facestoupdate++].mbr = nface1->bbox;
3553  }
3554  if ( oldedge->face_right > 0
3555  /* no need to update twice the same face.. */
3556  && oldedge->face_right != oldedge->face_left )
3557  {
3558  nface2 = lwt_GetFaceGeometry(topo, oldedge->face_right);
3559  if ( ! nface2 )
3560  {
3561  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3562  LWTFMT_ELEMID ", on the right of edge %" LWTFMT_ELEMID,
3563  oldedge->face_right, edge_id);
3564  return -1;
3565  }
3566  #if 0
3567  {
3568  size_t sz;
3569  char *wkt = lwgeom_to_wkt(nface2, WKT_EXTENDED, 2, &sz);
3570  LWDEBUGF(1, "new geometry of face right (%d): %s", (int)oldedge->face_right, wkt);
3571  lwfree(wkt);
3572  }
3573  #endif
3574  lwgeom_add_bbox(nface2);
3575  if ( ! nface2->bbox )
3576  {
3577  lwerror("Corrupted topology: face %d, right of edge %d, has no bbox",
3578  oldedge->face_right, edge_id);
3579  return -1;
3580  }
3581  faces[facestoupdate].face_id = oldedge->face_right;
3582  faces[facestoupdate++].mbr = nface2->bbox; /* ownership left to nface */
3583  }
3584  LWDEBUGF(1, "%d faces to update", facestoupdate);
3585  if ( facestoupdate )
3586  {
3587  uint64_t updatedFaces = lwt_be_updateFacesById(topo, &(faces[0]), facestoupdate);
3588  if (updatedFaces != facestoupdate)
3589  {
3590  if (nface1)
3591  lwgeom_free(nface1);
3592  if (nface2)
3593  lwgeom_free(nface2);
3594  _lwt_release_edges(oldedge, 1);
3595  if (updatedFaces == UINT64_MAX)
3596  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3597  else
3598  lwerror("Unexpected error: %d faces found when expecting 1", i);
3599  return -1;
3600  }
3601  }
3602  if ( nface1 ) lwgeom_free(nface1);
3603  if ( nface2 ) lwgeom_free(nface2);
3604  }
3605  else
3606  {
3607  LWDEBUG(1, "BBOX of changed edge did not change");
3608  }
3609 
3610  LWDEBUG(1, "all done, cleaning up edges");
3611 
3612  _lwt_release_edges(oldedge, 1);
3613  return 0; /* success */
3614 }
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: gbox.c:164
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
Definition: gbox.c:135
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define WKT_EXTENDED
Definition: liblwgeom.h:2167
int lwgeom_is_simple(const LWGEOM *lwgeom)
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
#define WKT_ISO
Definition: liblwgeom.h:2165
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:704
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:726
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:678
int ptarray_contains_point_partial(const POINTARRAY *pa, const POINT2D *pt, int check_closed, int *winding_number)
Definition: ptarray.c:746
#define LW_INSIDE
Constants for point-in-polygon return values.
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1034
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:50
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
static uint64_t lwt_be_updateFacesById(LWT_TOPOLOGY *topo, const LWT_ISO_FACE *faces, uint64_t numfaces)
Definition: lwgeom_topo.c:291
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:119
static int _lwt_InitEdgeEndByLine(edgeend *fee, edgeend *lee, LWLINE *edge, POINT2D *fp, POINT2D *lp)
Definition: lwgeom_topo.c:1472
LWT_ISO_EDGE * lwt_be_getEdgeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
Definition: lwgeom_topo.c:220
static int _lwt_GetInteriorEdgePoint(const LWLINE *edge, POINT2D *ip)
Definition: lwgeom_topo.c:1749
LWGEOM * lwt_GetFaceGeometry(LWT_TOPOLOGY *topo, LWT_ELEMID faceid)
Return the geometry of a face.
Definition: lwgeom_topo.c:2841
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:605
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:469
static int _lwt_FindAdjacentEdges(LWT_TOPOLOGY *topo, LWT_ELEMID node, edgeend *data, edgeend *other, int myedge_id)
Definition: lwgeom_topo.c:1530
static LWT_ISO_NODE * lwt_be_getNodeWithinBox2D(const LWT_TOPOLOGY *topo, const GBOX *box, uint64_t *numelems, int fields, uint64_t limit)
Definition: lwgeom_topo.c:172
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:459
static int lwt_be_updateEdgesById(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *edges, int numedges, int upd_fields)
Definition: lwgeom_topo.c:299
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
tuple res
Definition: window.py:79
GBOX * bbox
Definition: liblwgeom.h:472
GBOX * bbox
Definition: liblwgeom.h:496
POINTARRAY * points
Definition: liblwgeom.h:497
POINTARRAY * point
Definition: liblwgeom.h:485
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:404
double x
Definition: liblwgeom.h:404
uint32_t npoints
Definition: liblwgeom.h:441
LWT_ELEMID nextCCW
Definition: lwgeom_topo.c:1416
LWT_ELEMID nextCW
Definition: lwgeom_topo.c:1412

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(), 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(), window::res, 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: