PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ptarray_substring()

POINTARRAY* ptarray_substring ( POINTARRAY pa,
double  d1,
double  d2,
double  tolerance 
)

@d1 start location (distance from start / total distance) @d2 end location (distance from start / total distance)

Parameters
tolerancesnap to vertices at locations < tolerance away from given ones

Definition at line 1079 of file ptarray.c.

1080 {
1081  POINTARRAY *dpa;
1082  POINT4D pt;
1083  POINT4D p1, p2;
1084  POINT4D *p1ptr=&p1; /* don't break strict-aliasing rule */
1085  POINT4D *p2ptr=&p2;
1086  int nsegs, i;
1087  double length, slength, tlength;
1088  int state = 0; /* 0=before, 1=inside */
1089 
1090  /*
1091  * Create a dynamic pointarray with an initial capacity
1092  * equal to full copy of input points
1093  */
1095 
1096  /* Compute total line length */
1097  length = ptarray_length_2d(ipa);
1098 
1099 
1100  LWDEBUGF(3, "Total length: %g", length);
1101 
1102 
1103  /* Get 'from' and 'to' lengths */
1104  from = length*from;
1105  to = length*to;
1106 
1107 
1108  LWDEBUGF(3, "From/To: %g/%g", from, to);
1109 
1110 
1111  tlength = 0;
1112  getPoint4d_p(ipa, 0, &p1);
1113  nsegs = ipa->npoints - 1;
1114  for ( i = 0; i < nsegs; i++ )
1115  {
1116  double dseg;
1117 
1118  getPoint4d_p(ipa, i+1, &p2);
1119 
1120 
1121  LWDEBUGF(3 ,"Segment %d: (%g,%g,%g,%g)-(%g,%g,%g,%g)",
1122  i, p1.x, p1.y, p1.z, p1.m, p2.x, p2.y, p2.z, p2.m);
1123 
1124 
1125  /* Find the length of this segment */
1126  slength = distance2d_pt_pt((POINT2D *)p1ptr, (POINT2D *)p2ptr);
1127 
1128  /*
1129  * We are before requested start.
1130  */
1131  if ( state == 0 ) /* before */
1132  {
1133 
1134  LWDEBUG(3, " Before start");
1135 
1136  if ( fabs ( from - ( tlength + slength ) ) <= tolerance )
1137  {
1138 
1139  LWDEBUG(3, " Second point is our start");
1140 
1141  /*
1142  * Second point is our start
1143  */
1144  ptarray_append_point(dpa, &p2, LW_FALSE);
1145  state=1; /* we're inside now */
1146  goto END;
1147  }
1148 
1149  else if ( fabs(from - tlength) <= tolerance )
1150  {
1151 
1152  LWDEBUG(3, " First point is our start");
1153 
1154  /*
1155  * First point is our start
1156  */
1157  ptarray_append_point(dpa, &p1, LW_FALSE);
1158 
1159  /*
1160  * We're inside now, but will check
1161  * 'to' point as well
1162  */
1163  state=1;
1164  }
1165 
1166  /*
1167  * Didn't reach the 'from' point,
1168  * nothing to do
1169  */
1170  else if ( from > tlength + slength ) goto END;
1171 
1172  else /* tlength < from < tlength+slength */
1173  {
1174 
1175  LWDEBUG(3, " Seg contains first point");
1176 
1177  /*
1178  * Our start is between first and
1179  * second point
1180  */
1181  dseg = (from - tlength) / slength;
1182 
1183  interpolate_point4d(&p1, &p2, &pt, dseg);
1184 
1185  ptarray_append_point(dpa, &pt, LW_FALSE);
1186 
1187  /*
1188  * We're inside now, but will check
1189  * 'to' point as well
1190  */
1191  state=1;
1192  }
1193  }
1194 
1195  if ( state == 1 ) /* inside */
1196  {
1197 
1198  LWDEBUG(3, " Inside");
1199 
1200  /*
1201  * 'to' point is our second point.
1202  */
1203  if ( fabs(to - ( tlength + slength ) ) <= tolerance )
1204  {
1205 
1206  LWDEBUG(3, " Second point is our end");
1207 
1208  ptarray_append_point(dpa, &p2, LW_FALSE);
1209  break; /* substring complete */
1210  }
1211 
1212  /*
1213  * 'to' point is our first point.
1214  * (should only happen if 'to' is 0)
1215  */
1216  else if ( fabs(to - tlength) <= tolerance )
1217  {
1218 
1219  LWDEBUG(3, " First point is our end");
1220 
1221  ptarray_append_point(dpa, &p1, LW_FALSE);
1222 
1223  break; /* substring complete */
1224  }
1225 
1226  /*
1227  * Didn't reach the 'end' point,
1228  * just copy second point
1229  */
1230  else if ( to > tlength + slength )
1231  {
1232  ptarray_append_point(dpa, &p2, LW_FALSE);
1233  goto END;
1234  }
1235 
1236  /*
1237  * 'to' point falls on this segment
1238  * Interpolate and break.
1239  */
1240  else if ( to < tlength + slength )
1241  {
1242 
1243  LWDEBUG(3, " Seg contains our end");
1244 
1245  dseg = (to - tlength) / slength;
1246  interpolate_point4d(&p1, &p2, &pt, dseg);
1247 
1248  ptarray_append_point(dpa, &pt, LW_FALSE);
1249 
1250  break;
1251  }
1252 
1253  else
1254  {
1255  LWDEBUG(3, "Unhandled case");
1256  }
1257  }
1258 
1259 
1260 END:
1261 
1262  tlength += slength;
1263  memcpy(&p1, &p2, sizeof(POINT4D));
1264  }
1265 
1266  LWDEBUGF(3, "Out of loop, ptarray has %d points", dpa->npoints);
1267 
1268  return dpa;
1269 }
#define LW_FALSE
Definition: liblwgeom.h:94
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2398
void interpolate_point4d(const POINT4D *A, const POINT4D *B, POINT4D *I, double F)
Find interpolation point I between point A and point B so that the len(AI) == len(AB)*F and I falls o...
Definition: lwgeom_api.c:649
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:166
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1832
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
Definition: ptarray.c:147
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t npoints
Definition: liblwgeom.h:427

References distance2d_pt_pt(), POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, getPoint4d_p(), interpolate_point4d(), LW_FALSE, LWDEBUG, LWDEBUGF, POINT4D::m, POINTARRAY::npoints, ptarray_append_point(), ptarray_construct_empty(), ptarray_length_2d(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by LWGEOM_line_substring().

Here is the call graph for this function:
Here is the caller graph for this function: