1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
/*!
\file lib/vector/Vlib/simple_features.c
\brief Vector library - OGC Simple Features Access
Higher level functions for reading/writing/manipulating vectors.
Note: <b>In progress!</b> Currently on GV_POINT, GV_LINE,
GV_BOUNDARY are supported.
\todo
- Vect_sfa_line_is_simple()
- Vect_sfa_line_srid()
- Vect_sfa_line_envelope()
- Vect_sfa_line_asbinary()
- Vect_sfa_line_is_empty()
- Vect_sfa_line_is_3d()
- Vect_sfa_line_is_measured()
- Vect_sfa_line_boundary()
Reference: http://www.opengeospatial.org/standards/sfa
(C) 2009, 2011-2013 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Martin Landa <landa.martin gmail.com>
*/
#include <stdio.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#ifdef HAVE_POSTGRES
#include "pg_local_proto.h"
#endif
#ifdef HAVE_OGR
#include <ogr_api.h>
#endif
static int check_sftype(const struct line_pnts *, int, SF_FeatureType, int);
static int get_sftype(const struct line_pnts *, int, int);
static void print_point(const struct line_pnts *, int, int, int, FILE *);
/*!
\brief Get SF type of given vector feature
List of supported feature types:
- GV_POINT -> SF_POINT
- GV_LINE -> SF_LINESTRING
- GV_LINE (closed) -> SF_LINEARRING
- GV_BOUNDARY -> SF_POLYGON
\param Points pointer to line_pnts structure
\param type feature type (see supported types above)
\param with_z WITH_Z for 3D data
\return SF type identificator (see list of supported types)
\return -1 on error
*/
SF_FeatureType Vect_sfa_get_line_type(const struct line_pnts *Points, int type,
int with_z)
{
return get_sftype(Points, type, with_z);
}
/*!
\brief Get relevant GV type
\param Map pointer to Map_info structure
\param type SF geometry type (SF_POINT, SF_LINESTRING, ...)
\return GV type
\return -1 on error
*/
int Vect_sfa_get_type(SF_FeatureType sftype)
{
switch (sftype) {
case SF_POINT:
case SF_POINT25D:
return GV_POINT;
case SF_LINESTRING:
case SF_LINESTRING25D:
case SF_LINEARRING:
return GV_LINE;
case SF_POLYGON:
case SF_POLYGON25D:
return GV_BOUNDARY;
default:
break;
}
return -1;
}
/*!
\brief Check SF type
E.g. if <em>type</em> is GV_LINE with two or more segments and the
start node is identical with the end node, and <em>sftype</em> is
SF_LINEARRING, functions returns 1, otherwise 0.
\param Points pointer to line_pnts structure
\param type feature type (GV_POINT, GV_LINE, ...)
\param sftype SF type to be checked (SF_POINT, SF_LINE, ...)
\param with_z non-zero value for 3D data
\return 1 if type is sftype
\return 0 type differs from sftype
*/
int Vect_sfa_check_line_type(const struct line_pnts *Points, int type,
SF_FeatureType sftype, int with_z)
{
return check_sftype(Points, type, sftype, with_z);
}
/*!
\brief Get geometry dimension
\param Points pointer to line_pnts structure
\param type feature type (GV_POINT, GV_LINE, ...)
\return 0 for GV_POINT
\return 1 for GV_LINE
\return 2 for GV_BOUNDARY
\return -1 unsupported feature type
*/
int Vect_sfa_line_dimension(int type)
{
if (type == GV_POINT)
return 0;
if (type == GV_LINE)
return 1;
if (type == GV_BOUNDARY)
return 2;
return -1;
}
/*!
\brief Get geometry type (string)
Supported types:
- GV_POINT -> SF_POINT -> "POINT"
- GV_LINE -> SF_LINESTRING -> "LINESTRING"
- GV_LINE (closed) -> SF_LINEARRING -> "LINEARRING"
- GV_BOUNDARY (closed) -> SF_POLYGON -> "POLYGON"
Note: Allocated string should be freed by G_free().
\param Points pointer to line_pnts structure (feature geometry)
\param type feature type (see supported types above)
\return geometry type string
\return NULL unsupported feature type
*/
char *Vect_sfa_line_geometry_type(const struct line_pnts *Points, int type)
{
SF_FeatureType sftype = Vect_sfa_get_line_type(Points, type, 0);
if (sftype == SF_POINT)
return G_store("POINT");
if (sftype == SF_LINESTRING)
return G_store("LINESTRING");
if (sftype == SF_LINEARRING)
return G_store("LINEARRING");
if (sftype == SF_POLYGON)
return G_store("POLYGON");
return NULL;
}
/*!
\brief Export geometry to Well-Known Text
\param Points pointer to line_pnts structure
\param type feature type
\param with_z non-zero value for 3D data
\param precision floating number precision
\param[out] file file where to write the output
\return 0 on success
\return -1 unsupported feature type
*/
int Vect_sfa_line_astext(const struct line_pnts *Points, int type, int with_z,
int precision, FILE *file)
{
int i, sftype;
sftype = Vect_sfa_get_line_type(Points, type, with_z);
switch (sftype) {
case SF_POINT: { /* point */
fprintf(file, "POINT(");
print_point(Points, 0, with_z, precision, file);
fprintf(file, ")\n");
break;
}
case SF_LINESTRING:
case SF_LINEARRING: /* line */ {
if (sftype == SF_LINESTRING)
fprintf(file, "LINESTRING(");
else
fprintf(file, "LINEARRING(");
for (i = 0; i < Points->n_points; i++) {
print_point(Points, i, with_z, precision, file);
if (i < Points->n_points - 1)
fprintf(file, ", ");
}
fprintf(file, ")\n");
break;
}
case SF_POLYGON: /* polygon */ {
/* write only outer/inner ring */
fprintf(file, "(");
for (i = 0; i < Points->n_points; i++) {
print_point(Points, i, with_z, precision, file);
if (i < Points->n_points - 1)
fprintf(file, ", ");
}
fprintf(file, ")");
break;
}
default: {
G_warning(_("Unknown Simple Features type (%d)"), sftype);
return -1;
}
}
fflush(file);
return 0;
}
/*!
\brief Check if feature is simple
\param Points pointer to line_pnts structure
\param type feature type (GV_POINT, GV_LINE, ...)
\return 1 feature simple
\return 0 feature not simple
\return -1 feature type not supported (GV_POINT, GV_CENTROID, ...)
\note Implementation is pending, now always returns 0
*/
int Vect_sfa_is_line_simple(const struct line_pnts *Points UNUSED,
int type UNUSED, int with_z UNUSED)
{
/* TODO:
SF_FeatureType sftype;
Vect_sfa_get_line_type(Points, type, with_z);
*/
return 0;
}
/*!
\brief Check if feature is closed
\param Points pointer to line_pnts structure
\param type feature type (GV_LINE or GV_BOUNDARY)
\return 1 feature closed
\return 0 feature not closed
\return -1 feature type not supported (GV_POINT, GV_CENTROID, ...)
*/
int Vect_sfa_is_line_closed(const struct line_pnts *Points, int type,
int with_z)
{
int npoints;
if (type & (GV_LINES)) {
npoints = Vect_get_num_line_points(Points);
if (npoints > 2 && Points->x[0] == Points->x[npoints - 1] &&
Points->y[0] == Points->y[npoints - 1]) {
if (!with_z)
return 1;
if (Points->z[0] == Points->z[npoints - 1])
return 1;
}
return 0;
}
return -1;
}
/*!
\brief Get number of simple features
For native format or PostGIS Topology returns -1
\param Map vector map
\return number of features
\return -1 on error
*/
int Vect_sfa_get_num_features(struct Map_info *Map)
{
int nfeat;
nfeat = 0;
if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_OGR_DIRECT) {
/* OGR */
#ifdef HAVE_OGR
const struct Format_info_ogr *ogr_info;
ogr_info = &(Map->fInfo.ogr);
if (!ogr_info->layer)
return -1;
return OGR_L_GetFeatureCount(ogr_info->layer, TRUE);
#else
G_fatal_error(_("GRASS is not compiled with OGR support"));
return -1;
#endif
}
else if (Map->format == GV_FORMAT_POSTGIS &&
!Map->fInfo.pg.toposchema_name) {
#ifdef HAVE_POSTGRES
/* PostGIS */
char stmt[DB_SQL_MAX];
const struct Format_info_pg *pg_info;
pg_info = &(Map->fInfo.pg);
if (!pg_info->conn || !pg_info->table_name) {
G_warning(_("No connection defined"));
return -1;
}
sprintf(stmt, "SELECT count(*) FROM \"%s\".\"%s\"",
pg_info->schema_name, pg_info->table_name);
nfeat = Vect__execute_get_value_pg(pg_info->conn, stmt);
if (nfeat < 0) {
G_warning(_("Unable to get number of simple features"));
return -1;
}
#else
G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
return -1;
#endif
}
else {
G_warning(_("Unable to report simple features for vector map <%s>"),
Vect_get_full_name(Map));
return -1;
}
return nfeat;
}
int check_sftype(const struct line_pnts *points, int type,
SF_FeatureType sftype, int with_z)
{
if (type == GV_POINT && sftype == SF_POINT) {
return 1;
}
if (type == GV_LINE) {
if (sftype == SF_LINESTRING)
return 1;
if (sftype == SF_LINEARRING &&
Vect_sfa_is_line_closed(points, type, with_z))
return 1;
}
if (type == GV_BOUNDARY) {
if (sftype == SF_POLYGON &&
Vect_sfa_is_line_closed(points, type, 0)) /* force 2D */
return 1;
}
return 0;
}
int get_sftype(const struct line_pnts *points, int type, int with_z)
{
if (check_sftype(points, type, SF_POINT, with_z))
return SF_POINT;
if (check_sftype(points, type, SF_LINEARRING, with_z))
return SF_LINEARRING;
if (check_sftype(points, type, SF_LINESTRING, with_z))
return SF_LINESTRING;
if (check_sftype(points, type, SF_POLYGON, with_z))
return SF_POLYGON;
return -1;
}
void print_point(const struct line_pnts *Points, int index, int with_z,
int precision, FILE *file)
{
fprintf(file, "%.*f %.*f", precision, Points->x[index], precision,
Points->y[index]);
if (with_z)
fprintf(file, " %.*f", precision, Points->z[index]);
}
|