PostGIS  2.4.9dev-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 3298 of file lwgeom_topo.c.

References _lwt_CheckEdgeCrossing(), _lwt_EdgeMotionArea(), _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_union(), LWT_ISO_NODE::geom, LWT_ISO_EDGE::geom, getPoint2d_p(), LWDEBUG, LWDEBUGF, lwerror(), lwfree(), LWGEOM2GEOS(), lwgeom_add_bbox(), lwgeom_free(), lwgeom_geos_errmsg, lwgeom_geos_error(), 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(), LWLINE::points, ptarray_isccw(), LWT_ISO_EDGE::start_node, WKT_EXTENDED, WKT_ISO, POINT2D::x, and POINT2D::y.

Referenced by lwt_AddPoint().

3299 {
3300  LWT_ISO_EDGE *oldedge;
3301  LWT_ISO_EDGE newedge;
3302  POINT2D p1, p2, pt;
3303  int i;
3304  int isclosed = 0;
3305 
3306  /* curve must be simple */
3307  if ( ! lwgeom_is_simple(lwline_as_lwgeom(geom)) )
3308  {
3309  lwerror("SQL/MM Spatial exception - curve not simple");
3310  return -1;
3311  }
3312 
3313  i = 1;
3314  oldedge = lwt_be_getEdgeById(topo, &edge_id, &i, LWT_COL_EDGE_ALL);
3315  if ( ! oldedge )
3316  {
3317  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3318  "lwt_be_getEdgeById returned NULL and set i=%d", i);
3319  if ( i == -1 )
3320  {
3321  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3322  return -1;
3323  }
3324  else if ( i == 0 )
3325  {
3326  lwerror("SQL/MM Spatial exception - non-existent edge %"
3327  LWTFMT_ELEMID, edge_id);
3328  return -1;
3329  }
3330  else
3331  {
3332  lwerror("Backend coding error: getEdgeById callback returned NULL "
3333  "but numelements output parameter has value %d "
3334  "(expected 0 or 1)", i);
3335  return -1;
3336  }
3337  }
3338 
3339  LWDEBUGF(1, "lwt_ChangeEdgeGeom: "
3340  "old edge has %d points, new edge has %d points",
3341  oldedge->geom->points->npoints, geom->points->npoints);
3342 
3343  /*
3344  * e) Check StartPoint consistency
3345  */
3346  getPoint2d_p(oldedge->geom->points, 0, &p1);
3347  getPoint2d_p(geom->points, 0, &pt);
3348  if ( ! p2d_same(&p1, &pt) )
3349  {
3350  _lwt_release_edges(oldedge, 1);
3351  lwerror("SQL/MM Spatial exception - "
3352  "start node not geometry start point.");
3353  return -1;
3354  }
3355 
3356  /*
3357  * f) Check EndPoint consistency
3358  */
3359  if ( oldedge->geom->points->npoints < 2 )
3360  {
3361  _lwt_release_edges(oldedge, 1);
3362  lwerror("Corrupted topology: edge %" LWTFMT_ELEMID
3363  " has less than 2 vertices", oldedge->edge_id);
3364  return -1;
3365  }
3366  getPoint2d_p(oldedge->geom->points, oldedge->geom->points->npoints-1, &p2);
3367  if ( geom->points->npoints < 2 )
3368  {
3369  _lwt_release_edges(oldedge, 1);
3370  lwerror("Invalid edge: less than 2 vertices");
3371  return -1;
3372  }
3373  getPoint2d_p(geom->points, geom->points->npoints-1, &pt);
3374  if ( ! p2d_same(&pt, &p2) )
3375  {
3376  _lwt_release_edges(oldedge, 1);
3377  lwerror("SQL/MM Spatial exception - "
3378  "end node not geometry end point.");
3379  return -1;
3380  }
3381 
3382  /* Not in the specs:
3383  * if the edge is closed, check we didn't change winding !
3384  * (should be part of isomorphism checking)
3385  */
3386  if ( oldedge->start_node == oldedge->end_node )
3387  {
3388  isclosed = 1;
3389 #if 1 /* TODO: this is actually bogus as a test */
3390  /* check for valid edge (distinct vertices must exist) */
3391  if ( ! _lwt_GetInteriorEdgePoint(geom, &pt) )
3392  {
3393  _lwt_release_edges(oldedge, 1);
3394  lwerror("Invalid edge (no two distinct vertices exist)");
3395  return -1;
3396  }
3397 #endif
3398 
3399  if ( ptarray_isccw(oldedge->geom->points) !=
3400  ptarray_isccw(geom->points) )
3401  {
3402  _lwt_release_edges(oldedge, 1);
3403  lwerror("Edge twist at node POINT(%g %g)", p1.x, p1.y);
3404  return -1;
3405  }
3406  }
3407 
3408  if ( _lwt_CheckEdgeCrossing(topo, oldedge->start_node,
3409  oldedge->end_node, geom, edge_id ) )
3410  {
3411  /* would have called lwerror already, leaking :( */
3412  _lwt_release_edges(oldedge, 1);
3413  return -1;
3414  }
3415 
3416  LWDEBUG(1, "lwt_ChangeEdgeGeom: "
3417  "edge crossing check passed ");
3418 
3419  /*
3420  * Not in the specs:
3421  * Check topological isomorphism
3422  */
3423 
3424  /* Check that the "motion range" doesn't include any node */
3425  // 1. compute combined bbox of old and new edge
3426  GBOX mbox; /* motion box */
3427  lwgeom_add_bbox((LWGEOM*)oldedge->geom); /* just in case */
3428  lwgeom_add_bbox((LWGEOM*)geom); /* just in case */
3429  gbox_union(oldedge->geom->bbox, geom->bbox, &mbox);
3430  // 2. fetch all nodes in the combined box
3431  LWT_ISO_NODE *nodes;
3432  int numnodes;
3433  nodes = lwt_be_getNodeWithinBox2D(topo, &mbox, &numnodes,
3434  LWT_COL_NODE_ALL, 0);
3435  LWDEBUGF(1, "lwt_be_getNodeWithinBox2D returned %d nodes", numnodes);
3436  if ( numnodes == -1 ) {
3437  _lwt_release_edges(oldedge, 1);
3438  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3439  return -1;
3440  }
3441  // 3. if any node beside endnodes are found:
3442  if ( numnodes > ( 1 + isclosed ? 0 : 1 ) )
3443  {{
3444  GEOSGeometry *oarea, *narea;
3445  const GEOSPreparedGeometry *oareap, *nareap;
3446 
3447  initGEOS(lwnotice, lwgeom_geos_error);
3448 
3449  oarea = _lwt_EdgeMotionArea(oldedge->geom, isclosed);
3450  if ( ! oarea )
3451  {
3452  _lwt_release_edges(oldedge, 1);
3453  lwerror("Could not compute edge motion area for old edge");
3454  return -1;
3455  }
3456 
3457  narea = _lwt_EdgeMotionArea(geom, isclosed);
3458  if ( ! narea )
3459  {
3460  GEOSGeom_destroy(oarea);
3461  _lwt_release_edges(oldedge, 1);
3462  lwerror("Could not compute edge motion area for new edge");
3463  return -1;
3464  }
3465 
3466  // 3.2. bail out if any node is in one and not the other
3467  oareap = GEOSPrepare( oarea );
3468  nareap = GEOSPrepare( narea );
3469  for (i=0; i<numnodes; ++i)
3470  {
3471  LWT_ISO_NODE *n = &(nodes[i]);
3472  GEOSGeometry *ngg;
3473  int ocont, ncont;
3474  size_t sz;
3475  char *wkt;
3476  if ( n->node_id == oldedge->start_node ) continue;
3477  if ( n->node_id == oldedge->end_node ) continue;
3478  ngg = LWGEOM2GEOS( lwpoint_as_lwgeom(n->geom) , 0);
3479  ocont = GEOSPreparedContains( oareap, ngg );
3480  ncont = GEOSPreparedContains( nareap, ngg );
3481  GEOSGeom_destroy(ngg);
3482  if (ocont == 2 || ncont == 2)
3483  {
3484  _lwt_release_nodes(nodes, numnodes);
3485  GEOSPreparedGeom_destroy(oareap);
3486  GEOSGeom_destroy(oarea);
3487  GEOSPreparedGeom_destroy(nareap);
3488  GEOSGeom_destroy(narea);
3489  lwerror("GEOS exception on PreparedContains: %s", lwgeom_geos_errmsg);
3490  return -1;
3491  }
3492  if (ocont != ncont)
3493  {
3494  GEOSPreparedGeom_destroy(oareap);
3495  GEOSGeom_destroy(oarea);
3496  GEOSPreparedGeom_destroy(nareap);
3497  GEOSGeom_destroy(narea);
3498  wkt = lwgeom_to_wkt(lwpoint_as_lwgeom(n->geom), WKT_ISO, 15, &sz);
3499  _lwt_release_nodes(nodes, numnodes);
3500  lwerror("Edge motion collision at %s", wkt);
3501  lwfree(wkt); /* would not necessarely reach this point */
3502  return -1;
3503  }
3504  }
3505  GEOSPreparedGeom_destroy(oareap);
3506  GEOSGeom_destroy(oarea);
3507  GEOSPreparedGeom_destroy(nareap);
3508  GEOSGeom_destroy(narea);
3509  }}
3510  if ( numnodes ) _lwt_release_nodes(nodes, numnodes);
3511 
3512  LWDEBUG(1, "nodes containment check passed");
3513 
3514  /*
3515  * Check edge adjacency before
3516  * TODO: can be optimized to gather azimuths of all edge ends once
3517  */
3518 
3519  edgeend span_pre, epan_pre;
3520  /* initialize span_pre.myaz and epan_pre.myaz with existing edge */
3521  i = _lwt_InitEdgeEndByLine(&span_pre, &epan_pre,
3522  oldedge->geom, &p1, &p2);
3523  if ( i ) return -1; /* lwerror should have been raised */
3524  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_pre,
3525  isclosed ? &epan_pre : NULL, edge_id );
3526  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_pre,
3527  isclosed ? &span_pre : NULL, edge_id );
3528 
3529  LWDEBUGF(1, "edges adjacent to old edge are %" LWTFMT_ELEMID
3530  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3531  " and %" LWTFMT_ELEMID " (last point)",
3532  span_pre.nextCW, span_pre.nextCCW,
3533  epan_pre.nextCW, epan_pre.nextCCW);
3534 
3535  /* update edge geometry */
3536  newedge.edge_id = edge_id;
3537  newedge.geom = geom;
3538  i = lwt_be_updateEdgesById(topo, &newedge, 1, LWT_COL_EDGE_GEOM);
3539  if ( i == -1 )
3540  {
3541  _lwt_release_edges(oldedge, 1);
3542  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3543  return -1;
3544  }
3545  if ( ! i )
3546  {
3547  _lwt_release_edges(oldedge, 1);
3548  lwerror("Unexpected error: %d edges updated when expecting 1", i);
3549  return -1;
3550  }
3551 
3552  /*
3553  * Check edge adjacency after
3554  */
3555  edgeend span_post, epan_post;
3556  i = _lwt_InitEdgeEndByLine(&span_post, &epan_post, geom, &p1, &p2);
3557  if ( i ) return -1; /* lwerror should have been raised */
3558  /* initialize epan_post.myaz and epan_post.myaz */
3559  i = _lwt_InitEdgeEndByLine(&span_post, &epan_post,
3560  geom, &p1, &p2);
3561  if ( i ) return -1; /* lwerror should have been raised */
3562  _lwt_FindAdjacentEdges( topo, oldedge->start_node, &span_post,
3563  isclosed ? &epan_post : NULL, edge_id );
3564  _lwt_FindAdjacentEdges( topo, oldedge->end_node, &epan_post,
3565  isclosed ? &span_post : NULL, edge_id );
3566 
3567  LWDEBUGF(1, "edges adjacent to new edge are %" LWTFMT_ELEMID
3568  " and %" LWTFMT_ELEMID " (first point), %" LWTFMT_ELEMID
3569  " and %" LWTFMT_ELEMID " (last point)",
3570  span_pre.nextCW, span_pre.nextCCW,
3571  epan_pre.nextCW, epan_pre.nextCCW);
3572 
3573 
3574  /* Bail out if next CW or CCW edge on start node changed */
3575  if ( span_pre.nextCW != span_post.nextCW ||
3576  span_pre.nextCCW != span_post.nextCCW )
3577  {{
3578  LWT_ELEMID nid = oldedge->start_node;
3579  _lwt_release_edges(oldedge, 1);
3580  lwerror("Edge changed disposition around start node %"
3581  LWTFMT_ELEMID, nid);
3582  return -1;
3583  }}
3584 
3585  /* Bail out if next CW or CCW edge on end node changed */
3586  if ( epan_pre.nextCW != epan_post.nextCW ||
3587  epan_pre.nextCCW != epan_post.nextCCW )
3588  {{
3589  LWT_ELEMID nid = oldedge->end_node;
3590  _lwt_release_edges(oldedge, 1);
3591  lwerror("Edge changed disposition around end node %"
3592  LWTFMT_ELEMID, nid);
3593  return -1;
3594  }}
3595 
3596  /*
3597  -- Update faces MBR of left and right faces
3598  -- TODO: think about ways to optimize this part, like see if
3599  -- the old edge geometry partecipated in the definition
3600  -- of the current MBR (for shrinking) or the new edge MBR
3601  -- would be larger than the old face MBR...
3602  --
3603  */
3604  int facestoupdate = 0;
3605  LWT_ISO_FACE faces[2];
3606  LWGEOM *nface1 = NULL;
3607  LWGEOM *nface2 = NULL;
3608  if ( oldedge->face_left != 0 )
3609  {
3610  nface1 = lwt_GetFaceGeometry(topo, oldedge->face_left);
3611  if ( ! nface1 )
3612  {
3613  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3614  LWTFMT_ELEMID ", on the left of edge %" LWTFMT_ELEMID,
3615  oldedge->face_left, edge_id);
3616  return -1;
3617  }
3618 #if 0
3619  {
3620  size_t sz;
3621  char *wkt = lwgeom_to_wkt(nface1, WKT_EXTENDED, 2, &sz);
3622  LWDEBUGF(1, "new geometry of face left (%d): %s", (int)oldedge->face_left, wkt);
3623  lwfree(wkt);
3624  }
3625 #endif
3626  lwgeom_add_bbox(nface1);
3627  faces[facestoupdate].face_id = oldedge->face_left;
3628  /* ownership left to nface */
3629  faces[facestoupdate++].mbr = nface1->bbox;
3630  }
3631  if ( oldedge->face_right != 0
3632  /* no need to update twice the same face.. */
3633  && oldedge->face_right != oldedge->face_left )
3634  {
3635  nface2 = lwt_GetFaceGeometry(topo, oldedge->face_right);
3636  if ( ! nface2 )
3637  {
3638  lwerror("lwt_ChangeEdgeGeom could not construct face %"
3639  LWTFMT_ELEMID ", on the right of edge %" LWTFMT_ELEMID,
3640  oldedge->face_right, edge_id);
3641  return -1;
3642  }
3643 #if 0
3644  {
3645  size_t sz;
3646  char *wkt = lwgeom_to_wkt(nface2, WKT_EXTENDED, 2, &sz);
3647  LWDEBUGF(1, "new geometry of face right (%d): %s", (int)oldedge->face_right, wkt);
3648  lwfree(wkt);
3649  }
3650 #endif
3651  lwgeom_add_bbox(nface2);
3652  faces[facestoupdate].face_id = oldedge->face_right;
3653  faces[facestoupdate++].mbr = nface2->bbox; /* ownership left to nface */
3654  }
3655  LWDEBUGF(1, "%d faces to update", facestoupdate);
3656  if ( facestoupdate )
3657  {
3658  i = lwt_be_updateFacesById( topo, &(faces[0]), facestoupdate );
3659  if ( i != facestoupdate )
3660  {
3661  if ( nface1 ) lwgeom_free(nface1);
3662  if ( nface2 ) lwgeom_free(nface2);
3663  _lwt_release_edges(oldedge, 1);
3664  if ( i == -1 )
3665  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
3666  else
3667  lwerror("Unexpected error: %d faces found when expecting 1", i);
3668  return -1;
3669  }
3670  }
3671  if ( nface1 ) lwgeom_free(nface1);
3672  if ( nface2 ) lwgeom_free(nface2);
3673 
3674  LWDEBUG(1, "all done, cleaning up edges");
3675 
3676  _lwt_release_edges(oldedge, 1);
3677  return 0; /* success */
3678 }
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:589
static void _lwt_release_nodes(LWT_ISO_NODE *nodes, int num_nodes)
Definition: lwgeom_topo.c:477
static int _lwt_GetInteriorEdgePoint(const LWLINE *edge, POINT2D *ip)
Definition: lwgeom_topo.c:1725
LWT_ELEMID face_left
GBOX * bbox
Definition: liblwgeom.h:398
GBOX * bbox
Definition: liblwgeom.h:420
static int lwt_be_updateEdgesById(LWT_TOPOLOGY *topo, const LWT_ISO_EDGE *edges, int numedges, int upd_fields)
Definition: lwgeom_topo.c:306
LWPOINT * geom
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:669
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
void lwfree(void *mem)
Definition: lwutil.c:244
int npoints
Definition: liblwgeom.h:371
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
LWLINE * geom
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
LWT_ISO_EDGE * lwt_be_getEdgeById(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, int *numelems, int fields)
Definition: lwgeom_topo.c:225
LWT_ELEMID nextCCW
Definition: lwgeom_topo.c:1393
double x
Definition: liblwgeom.h:328
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:1029
#define WKT_ISO
Definition: liblwgeom.h:2083
static int lwt_be_updateFacesById(LWT_TOPOLOGY *topo, const LWT_ISO_FACE *faces, int numfaces)
Definition: lwgeom_topo.c:298
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:298
void lwgeom_geos_error(const char *fmt,...)
const LWT_BE_IFACE * be_iface
LWT_ELEMID face_id
LWT_ELEMID face_right
static GEOSGeometry * _lwt_EdgeMotionArea(LWLINE *geom, int isclosed)
Definition: lwgeom_topo.c:3242
#define LWT_COL_EDGE_ALL
LWT_ELEMID node_id
double y
Definition: liblwgeom.h:328
LWT_ELEMID edge_id
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:347
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
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:146
#define WKT_EXTENDED
Definition: liblwgeom.h:2085
static int _lwt_FindAdjacentEdges(LWT_TOPOLOGY *topo, LWT_ELEMID node, edgeend *data, edgeend *other, int myedge_id)
Definition: lwgeom_topo.c:1507
LWGEOM * lwt_GetFaceGeometry(LWT_TOPOLOGY *topo, LWT_ELEMID faceid)
Return the geometry of a face.
Definition: lwgeom_topo.c:2858
#define LWT_COL_NODE_ALL
LWT_ELEMID start_node
int lwgeom_is_simple(const LWGEOM *lwgeom)
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:467
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:648
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:303
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_GEOM
static int _lwt_InitEdgeEndByLine(edgeend *fee, edgeend *lee, LWLINE *edge, POINT2D *fp, POINT2D *lp)
Definition: lwgeom_topo.c:1449
LWT_ELEMID nextCW
Definition: lwgeom_topo.c:1389
LWT_ELEMID end_node
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
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
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:120
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:44
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: