PostGIS  3.0.0dev-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 3170 of file lwgeom_topo.c.

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().

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