PostGIS  3.0.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 3187 of file lwgeom_topo.c.

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

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