static gaiaDynamicLinePtr auxGridSnapRing (gaiaRingPtr rng, double origin_x, double origin_y, double origin_z, double origin_m, double size_x, double size_y, double size_z, double size_m) { /* snapping a Ring to a regular Grid */ double x; double y; double z; double m; int has_z = 0; int has_m = 0; int iv; gaiaDynamicLinePtr dyn; gaiaPointPtr pt0; gaiaPointPtr pt; int count = 0; if (rng == NULL) return NULL; if (rng->DimensionModel == GAIA_XY_Z || rng->DimensionModel == GAIA_XY_Z_M) has_z = 1; if (rng->DimensionModel == GAIA_XY_M || rng->DimensionModel == GAIA_XY_Z_M) has_m = 1; dyn = gaiaAllocDynamicLine (); for (iv = 0; iv < rng->Points; iv++) { /* snapping each Vertex to the given grid */ int to_be_inserted = 0; z = 0.0; m = 0.0; if (has_z && has_m) { gaiaGetPointXYZM (rng->Coords, iv, &x, &y, &z, &m); } else if (has_z) { gaiaGetPointXYZ (rng->Coords, iv, &x, &y, &z); } else if (has_m) { gaiaGetPointXYM (rng->Coords, iv, &x, &y, &m); } else { gaiaGetPoint (rng->Coords, iv, &x, &y); } /* snapping coords to the given grid */ if (size_x > 0.0) x = rint ((x - origin_x) / size_x) * size_x + origin_x; if (size_y > 0.0) y = rint ((y - origin_y) / size_y) * size_y + origin_y; if (has_z && size_z > 0.0) z = rint ((z - origin_z) / size_z) * size_z + origin_z; if (has_m && size_m > 0.0) m = rint ((m - origin_m) / size_m) * size_m + origin_m; if (dyn->Last == NULL) to_be_inserted = 1; else { /* skipping repeated points */ pt = dyn->Last; if (has_z && has_m) { if (pt->X == x && pt->Y == y && pt->Z == z && pt->M == m) ; else to_be_inserted = 1; } else if (has_z) { if (pt->X == x && pt->Y == y && pt->Z == z) ; else to_be_inserted = 1; } else if (has_m) { if (pt->X == x && pt->Y == y && pt->M == m) ; else to_be_inserted = 1; } else { if (pt->X == x && pt->Y == y) ; else to_be_inserted = 1; } } if (to_be_inserted) { if (has_z && has_m) gaiaAppendPointZMToDynamicLine (dyn, x, y, z, m); else if (has_z) gaiaAppendPointZToDynamicLine (dyn, x, y, z); else if (has_m) gaiaAppendPointMToDynamicLine (dyn, x, y, m); else gaiaAppendPointToDynamicLine (dyn, x, y); } } /* ensuring for Ring closure */ pt0 = dyn->First; pt = dyn->Last; if (has_z && has_m) { if (pt0->X == pt->X && pt0->Y == pt->Y && pt0->Z == pt->Z && pt0->M == pt->M) ; else gaiaAppendPointZMToDynamicLine (dyn, pt->X, pt->Y, pt->Z, pt->M); } else if (has_z) { if (pt0->X == pt->X && pt0->Y == pt->Y && pt0->Z == pt->Z) ; else gaiaAppendPointZToDynamicLine (dyn, pt->X, pt->Y, pt->Z); } else if (has_m) { if (pt0->X == pt->X && pt0->Y == pt->Y && pt0->M == pt->M) ; else gaiaAppendPointMToDynamicLine (dyn, pt->X, pt->Y, pt->M); } else { if (pt0->X == pt->X && pt0->Y == pt->Y) ; else gaiaAppendPointToDynamicLine (dyn, pt->X, pt->Y); } /* checking for validity */ pt = dyn->First; while (pt) { /* counting how many points are there */ count++; pt = pt->Next; } if (count < 4) { /* skipping any collapsed ring */ gaiaFreeDynamicLine (dyn); return NULL; } return dyn; }
static void kml_add_point_to_lineZ (gaiaDynamicLinePtr dyn, double x, double y, double z) { /* appending a point */ gaiaAppendPointZToDynamicLine (dyn, x, y, z); }
static void auxGridSnapLinestring (gaiaLinestringPtr ln, gaiaGeomCollPtr result, double origin_x, double origin_y, double origin_z, double origin_m, double size_x, double size_y, double size_z, double size_m) { /* snapping a Linestring to a regular Grid */ double x; double y; double z; double m; int has_z = 0; int has_m = 0; int iv; gaiaDynamicLinePtr dyn; gaiaPointPtr pt; gaiaLinestringPtr lnx; int count = 0; if (ln == NULL || result == NULL) return; if (ln->DimensionModel == GAIA_XY_Z || ln->DimensionModel == GAIA_XY_Z_M) has_z = 1; if (ln->DimensionModel == GAIA_XY_M || ln->DimensionModel == GAIA_XY_Z_M) has_m = 1; dyn = gaiaAllocDynamicLine (); for (iv = 0; iv < ln->Points; iv++) { /* snapping each Vertex to the given grid */ int to_be_inserted = 0; z = 0.0; m = 0.0; if (has_z && has_m) { gaiaGetPointXYZM (ln->Coords, iv, &x, &y, &z, &m); } else if (has_z) { gaiaGetPointXYZ (ln->Coords, iv, &x, &y, &z); } else if (has_m) { gaiaGetPointXYM (ln->Coords, iv, &x, &y, &m); } else { gaiaGetPoint (ln->Coords, iv, &x, &y); } /* snapping coords to the given grid */ if (size_x > 0.0) x = rint ((x - origin_x) / size_x) * size_x + origin_x; if (size_y > 0.0) y = rint ((y - origin_y) / size_y) * size_y + origin_y; if (has_z && size_z > 0.0) z = rint ((z - origin_z) / size_z) * size_z + origin_z; if (has_m && size_m > 0.0) m = rint ((m - origin_m) / size_m) * size_m + origin_m; if (dyn->Last == NULL) to_be_inserted = 1; else { /* skipping repeated points */ pt = dyn->Last; if (has_z && has_m) { if (pt->X == x && pt->Y == y && pt->Z == z && pt->M == m) ; else to_be_inserted = 1; } else if (has_z) { if (pt->X == x && pt->Y == y && pt->Z == z) ; else to_be_inserted = 1; } else if (has_m) { if (pt->X == x && pt->Y == y && pt->M == m) ; else to_be_inserted = 1; } else { if (pt->X == x && pt->Y == y) ; else to_be_inserted = 1; } } if (to_be_inserted) { if (has_z && has_m) gaiaAppendPointZMToDynamicLine (dyn, x, y, z, m); else if (has_z) gaiaAppendPointZToDynamicLine (dyn, x, y, z); else if (has_m) gaiaAppendPointMToDynamicLine (dyn, x, y, m); else gaiaAppendPointToDynamicLine (dyn, x, y); } } /* checking for validity */ pt = dyn->First; while (pt) { /* counting how many points are there */ count++; pt = pt->Next; } if (count < 2) { /* skipping any collapsed line */ gaiaFreeDynamicLine (dyn); return; } /* inserting into the result Geometry */ lnx = gaiaAddLinestringToGeomColl (result, count); iv = 0; pt = dyn->First; while (pt) { /* copying points */ if (lnx->DimensionModel == GAIA_XY_Z) { gaiaSetPointXYZ (lnx->Coords, iv, pt->X, pt->Y, pt->Z); } else if (lnx->DimensionModel == GAIA_XY_M) { gaiaSetPointXYM (lnx->Coords, iv, pt->X, pt->Y, pt->M); } else if (lnx->DimensionModel == GAIA_XY_Z_M) { gaiaSetPointXYZM (lnx->Coords, iv, pt->X, pt->Y, pt->Z, pt->M); } else { gaiaSetPoint (lnx->Coords, iv, pt->X, pt->Y); } iv++; pt = pt->Next; } gaiaFreeDynamicLine (dyn); }