PostGIS  3.1.6dev-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 3241 of file lwgeom_topo.c.

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

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: