PostGIS  2.2.8dev-r@@SVN_REVISION@@
shp2pgsql-core.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2008 OpenGeo.org
7  * Copyright (C) 2009 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU General Public Licence. See the COPYING file.
11  *
12  * Maintainer: Paul Ramsey <pramsey@opengeo.org>
13  *
14  **********************************************************************/
15 
16 #include "../postgis_config.h"
17 
18 #include "shp2pgsql-core.h"
19 #include "../liblwgeom/liblwgeom.h"
20 #include "../liblwgeom/lwgeom_log.h" /* for LWDEBUG macros */
21 
22 
23 
24 /* Internal ring/point structures */
25 typedef struct struct_point
26 {
27  double x, y, z, m;
28 } Point;
29 
30 typedef struct struct_ring
31 {
32  Point *list; /* list of points */
33  struct struct_ring *next;
34  int n; /* number of points in list */
35  unsigned int linked; /* number of "next" rings */
36 } Ring;
37 
38 
39 /*
40  * Internal functions
41  */
42 
43 #define UTF8_GOOD_RESULT 0
44 #define UTF8_BAD_RESULT 1
45 #define UTF8_NO_RESULT 2
46 
47 char *escape_copy_string(char *str);
48 char *escape_insert_string(char *str);
49 
50 int GeneratePointGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry, int force_multi);
51 int GenerateLineStringGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry);
52 int PIP(Point P, Point *V, int n);
53 int FindPolygons(SHPObject *obj, Ring ***Out);
54 void ReleasePolygons(Ring **polys, int npolys);
55 int GeneratePolygonGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry);
56 
57 
58 /* Return allocated string containing UTF8 string converted from encoding fromcode */
59 static int
60 utf8(const char *fromcode, char *inputbuf, char **outputbuf)
61 {
62  iconv_t cd;
63  char *outputptr;
64  size_t outbytesleft;
65  size_t inbytesleft;
66 
67  inbytesleft = strlen(inputbuf);
68 
69  cd = iconv_open("UTF-8", fromcode);
70  if ( cd == ((iconv_t)(-1)) )
71  return UTF8_NO_RESULT;
72 
73  outbytesleft = inbytesleft * 3 + 1; /* UTF8 string can be 3 times larger */
74  /* then local string */
75  *outputbuf = (char *)malloc(outbytesleft);
76  if (!*outputbuf)
77  return UTF8_NO_RESULT;
78 
79  memset(*outputbuf, 0, outbytesleft);
80  outputptr = *outputbuf;
81 
82  /* Does this string convert cleanly? */
83  if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
84  {
85 #ifdef HAVE_ICONVCTL
86  int on = 1;
87  /* No. Try to convert it while transliterating. */
88  iconvctl(cd, ICONV_SET_TRANSLITERATE, &on);
89  if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
90  {
91  /* No. Try to convert it while discarding errors. */
92  iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &on);
93  if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
94  {
95  /* Still no. Throw away the buffer and return. */
96  free(*outputbuf);
97  iconv_close(cd);
98  return UTF8_NO_RESULT;
99  }
100  }
101  iconv_close(cd);
102  return UTF8_BAD_RESULT;
103 #else
104  free(*outputbuf);
105  iconv_close(cd);
106  return UTF8_NO_RESULT;
107 #endif
108  }
109  /* Return a good result, converted string is in buffer. */
110  iconv_close(cd);
111  return UTF8_GOOD_RESULT;
112 }
113 
118 char *
120 {
121  /*
122  * Escape the following characters by adding a preceding backslash
123  * tab, backslash, cr, lf
124  *
125  * 1. find # of escaped characters
126  * 2. make new string
127  *
128  */
129 
130  char *result;
131  char *ptr, *optr;
132  int toescape = 0;
133  size_t size;
134 
135  ptr = str;
136 
137  /* Count how many characters we need to escape so we know the size of the string we need to return */
138  while (*ptr)
139  {
140  if (*ptr == '\t' || *ptr == '\\' || *ptr == '\n' || *ptr == '\r')
141  toescape++;
142 
143  ptr++;
144  }
145 
146  /* If we don't have to escape anything, simply return the input pointer */
147  if (toescape == 0)
148  return str;
149 
150  size = ptr - str + toescape + 1;
151  result = calloc(1, size);
152  optr = result;
153  ptr = str;
154 
155  while (*ptr)
156  {
157  if ( *ptr == '\t' || *ptr == '\\' || *ptr == '\n' || *ptr == '\r' )
158  *optr++ = '\\';
159 
160  *optr++ = *ptr++;
161  }
162 
163  *optr = '\0';
164 
165  return result;
166 }
167 
168 
173 char *
175 {
176  /*
177  * Escape single quotes by adding a preceding single quote
178  *
179  * 1. find # of characters
180  * 2. make new string
181  */
182 
183  char *result;
184  char *ptr, *optr;
185  int toescape = 0;
186  size_t size;
187 
188  ptr = str;
189 
190  /* Count how many characters we need to escape so we know the size of the string we need to return */
191  while (*ptr)
192  {
193  if (*ptr == '\'')
194  toescape++;
195 
196  ptr++;
197  }
198 
199  /* If we don't have to escape anything, simply return the input pointer */
200  if (toescape == 0)
201  return str;
202 
203  size = ptr - str + toescape + 1;
204  result = calloc(1, size);
205  optr = result;
206  ptr = str;
207 
208  while (*ptr)
209  {
210  if (*ptr == '\'')
211  *optr++='\'';
212 
213  *optr++ = *ptr++;
214  }
215 
216  *optr='\0';
217 
218  return result;
219 }
220 
221 
226 int
227 GeneratePointGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry, int force_multi)
228 {
229  LWGEOM **lwmultipoints;
230  LWGEOM *lwgeom = NULL;
231 
232  POINT4D point4d;
233 
234  int dims = 0;
235  int u;
236 
237  char *mem;
238  size_t mem_length;
239 
240  FLAGS_SET_Z(dims, state->has_z);
241  FLAGS_SET_M(dims, state->has_m);
242 
243  /* Allocate memory for our array of LWPOINTs and our dynptarrays */
244  lwmultipoints = malloc(sizeof(LWPOINT *) * obj->nVertices);
245 
246  /* We need an array of pointers to each of our sub-geometries */
247  for (u = 0; u < obj->nVertices; u++)
248  {
249  /* Create a ptarray containing a single point */
250  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, 1);
251 
252  /* Generate the point */
253  point4d.x = obj->padfX[u];
254  point4d.y = obj->padfY[u];
255 
256  if (state->has_z)
257  point4d.z = obj->padfZ[u];
258  if (state->has_m)
259  point4d.m = obj->padfM[u];
260 
261  /* Add in the point! */
262  ptarray_append_point(pa, &point4d, LW_TRUE);
263 
264  /* Generate the LWPOINT */
265  lwmultipoints[u] = lwpoint_as_lwgeom(lwpoint_construct(state->from_srid, NULL, pa));
266  }
267 
268  /* If we have more than 1 vertex then we are working on a MULTIPOINT and so generate a MULTIPOINT
269  rather than a POINT */
270  if ((obj->nVertices > 1) || force_multi)
271  {
272  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTIPOINTTYPE, state->from_srid, NULL, obj->nVertices, lwmultipoints));
273  }
274  else
275  {
276  lwgeom = lwmultipoints[0];
277  lwfree(lwmultipoints);
278  }
279 
280  if (state->config->use_wkt)
281  {
282  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
283  }
284  else
285  {
286  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
287  }
288 
289  if ( !mem )
290  {
291  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
292  return SHPLOADERERR;
293  }
294 
295  /* Free all of the allocated items */
296  lwgeom_free(lwgeom);
297 
298  /* Return the string - everything ok */
299  *geometry = mem;
300 
301  return SHPLOADEROK;
302 }
303 
304 
308 int
310 {
311 
312  LWGEOM **lwmultilinestrings;
313  LWGEOM *lwgeom = NULL;
314  POINT4D point4d;
315  int dims = 0;
316  int u, v, start_vertex, end_vertex;
317  char *mem;
318  size_t mem_length;
319 
320 
321  FLAGS_SET_Z(dims, state->has_z);
322  FLAGS_SET_M(dims, state->has_m);
323 
324  if (state->config->simple_geometries == 1 && obj->nParts > 1)
325  {
326  snprintf(state->message, SHPLOADERMSGLEN, _("We have a Multilinestring with %d parts, can't use -S switch!"), obj->nParts);
327 
328  return SHPLOADERERR;
329  }
330 
331  /* Allocate memory for our array of LWLINEs and our dynptarrays */
332  lwmultilinestrings = malloc(sizeof(LWPOINT *) * obj->nParts);
333 
334  /* We need an array of pointers to each of our sub-geometries */
335  for (u = 0; u < obj->nParts; u++)
336  {
337  /* Create a ptarray containing the line points */
338  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, obj->nParts);
339 
340  /* Set the start/end vertices depending upon whether this is
341  a MULTILINESTRING or not */
342  if ( u == obj->nParts-1 )
343  end_vertex = obj->nVertices;
344  else
345  end_vertex = obj->panPartStart[u + 1];
346 
347  start_vertex = obj->panPartStart[u];
348 
349  for (v = start_vertex; v < end_vertex; v++)
350  {
351  /* Generate the point */
352  point4d.x = obj->padfX[v];
353  point4d.y = obj->padfY[v];
354 
355  if (state->has_z)
356  point4d.z = obj->padfZ[v];
357  if (state->has_m)
358  point4d.m = obj->padfM[v];
359 
360  ptarray_append_point(pa, &point4d, LW_FALSE);
361  }
362 
363  /* Generate the LWLINE */
364  lwmultilinestrings[u] = lwline_as_lwgeom(lwline_construct(state->from_srid, NULL, pa));
365  }
366 
367  /* If using MULTILINESTRINGs then generate the serialized collection, otherwise just a single LINESTRING */
368  if (state->config->simple_geometries == 0)
369  {
370  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTILINETYPE, state->from_srid, NULL, obj->nParts, lwmultilinestrings));
371  }
372  else
373  {
374  lwgeom = lwmultilinestrings[0];
375  lwfree(lwmultilinestrings);
376  }
377 
378  if (!state->config->use_wkt)
379  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
380  else
381  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
382 
383  if ( !mem )
384  {
385  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
386  return SHPLOADERERR;
387  }
388 
389  /* Free all of the allocated items */
390  lwgeom_free(lwgeom);
391 
392  /* Return the string - everything ok */
393  *geometry = mem;
394 
395  return SHPLOADEROK;
396 }
397 
398 
405 int
406 PIP(Point P, Point *V, int n)
407 {
408  int cn = 0; /* the crossing number counter */
409  int i;
410 
411  /* loop through all edges of the polygon */
412  for (i = 0; i < n-1; i++) /* edge from V[i] to V[i+1] */
413  {
414  if (((V[i].y <= P.y) && (V[i + 1].y > P.y)) /* an upward crossing */
415  || ((V[i].y > P.y) && (V[i + 1].y <= P.y))) /* a downward crossing */
416  {
417  double vt = (float)(P.y - V[i].y) / (V[i + 1].y - V[i].y);
418  if (P.x < V[i].x + vt * (V[i + 1].x - V[i].x)) /* P.x < intersect */
419  ++cn; /* a valid crossing of y=P.y right of P.x */
420  }
421  }
422 
423  return (cn&1); /* 0 if even (out), and 1 if odd (in) */
424 }
425 
426 
427 int
429 {
430  Ring **Outer; /* Pointers to Outer rings */
431  int out_index=0; /* Count of Outer rings */
432  Ring **Inner; /* Pointers to Inner rings */
433  int in_index=0; /* Count of Inner rings */
434  int pi; /* part index */
435 
436 #if POSTGIS_DEBUG_LEVEL > 0
437  static int call = -1;
438  call++;
439 #endif
440 
441  LWDEBUGF(4, "FindPolygons[%d]: allocated space for %d rings\n", call, obj->nParts);
442 
443  /* Allocate initial memory */
444  Outer = (Ring **)malloc(sizeof(Ring *) * obj->nParts);
445  Inner = (Ring **)malloc(sizeof(Ring *) * obj->nParts);
446 
447  /* Iterate over rings dividing in Outers and Inners */
448  for (pi=0; pi < obj->nParts; pi++)
449  {
450  int vi; /* vertex index */
451  int vs; /* start index */
452  int ve; /* end index */
453  int nv; /* number of vertex */
454  double area = 0.0;
455  Ring *ring;
456 
457  /* Set start and end vertexes */
458  if (pi == obj->nParts - 1)
459  ve = obj->nVertices;
460  else
461  ve = obj->panPartStart[pi + 1];
462 
463  vs = obj->panPartStart[pi];
464 
465  /* Compute number of vertexes */
466  nv = ve - vs;
467 
468  /* Allocate memory for a ring */
469  ring = (Ring *)malloc(sizeof(Ring));
470  ring->list = (Point *)malloc(sizeof(Point) * nv);
471  ring->n = nv;
472  ring->next = NULL;
473  ring->linked = 0;
474 
475  /* Iterate over ring vertexes */
476  for (vi = vs; vi < ve; vi++)
477  {
478  int vn = vi+1; /* next vertex for area */
479  if (vn == ve)
480  vn = vs;
481 
482  ring->list[vi - vs].x = obj->padfX[vi];
483  ring->list[vi - vs].y = obj->padfY[vi];
484  ring->list[vi - vs].z = obj->padfZ[vi];
485  ring->list[vi - vs].m = obj->padfM[vi];
486 
487  area += (obj->padfX[vi] * obj->padfY[vn]) -
488  (obj->padfY[vi] * obj->padfX[vn]);
489  }
490 
491  /* Close the ring with first vertex */
492  /*ring->list[vi].x = obj->padfX[vs]; */
493  /*ring->list[vi].y = obj->padfY[vs]; */
494  /*ring->list[vi].z = obj->padfZ[vs]; */
495  /*ring->list[vi].m = obj->padfM[vs]; */
496 
497  /* Clockwise (or single-part). It's an Outer Ring ! */
498  if (area < 0.0 || obj->nParts == 1)
499  {
500  Outer[out_index] = ring;
501  out_index++;
502  }
503  else
504  {
505  /* Counterclockwise. It's an Inner Ring ! */
506  Inner[in_index] = ring;
507  in_index++;
508  }
509  }
510 
511  LWDEBUGF(4, "FindPolygons[%d]: found %d Outer, %d Inners\n", call, out_index, in_index);
512 
513  /* Put the inner rings into the list of the outer rings */
514  /* of which they are within */
515  for (pi = 0; pi < in_index; pi++)
516  {
517  Point pt, pt2;
518  int i;
519  Ring *inner = Inner[pi], *outer = NULL;
520 
521  pt.x = inner->list[0].x;
522  pt.y = inner->list[0].y;
523 
524  pt2.x = inner->list[1].x;
525  pt2.y = inner->list[1].y;
526 
527  /*
528  * If we assume that the case of the "big polygon w/o hole
529  * containing little polygon w/ hold" is ordered so that the
530  * big polygon comes first, then checking the list in reverse
531  * will assign the little polygon's hole to the little polygon
532  * w/o a lot of extra fancy containment logic here
533  */
534  for (i = out_index - 1; i >= 0; i--)
535  {
536  int in;
537 
538  in = PIP(pt, Outer[i]->list, Outer[i]->n);
539  if ( in || PIP(pt2, Outer[i]->list, Outer[i]->n) )
540  {
541  outer = Outer[i];
542  break;
543  }
544  }
545 
546  if (outer)
547  {
548  outer->linked++;
549  while (outer->next)
550  outer = outer->next;
551 
552  outer->next = inner;
553  }
554  else
555  {
556  /* The ring wasn't within any outer rings, */
557  /* assume it is a new outer ring. */
558  LWDEBUGF(4, "FindPolygons[%d]: hole %d is orphan\n", call, pi);
559 
560  Outer[out_index] = inner;
561  out_index++;
562  }
563  }
564 
565  *Out = Outer;
566  /*
567  * Only free the containing Inner array, not the ring elements, because
568  * the rings are now owned by the linked lists in the Outer array elements.
569  */
570  free(Inner);
571 
572  return out_index;
573 }
574 
575 
576 void
577 ReleasePolygons(Ring **polys, int npolys)
578 {
579  int pi;
580 
581  /* Release all memory */
582  for (pi = 0; pi < npolys; pi++)
583  {
584  Ring *Poly, *temp;
585  Poly = polys[pi];
586  while (Poly != NULL)
587  {
588  temp = Poly;
589  Poly = Poly->next;
590  free(temp->list);
591  free(temp);
592  }
593  }
594 
595  free(polys);
596 }
597 
598 
606 int
607 GeneratePolygonGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
608 {
609  Ring **Outer;
610  int polygon_total, ring_total;
611  int pi, vi; /* part index and vertex index */
612 
613  LWGEOM **lwpolygons;
614  LWGEOM *lwgeom;
615 
616  POINT4D point4d;
617 
618  int dims = 0;
619 
620  char *mem;
621  size_t mem_length;
622 
623  FLAGS_SET_Z(dims, state->has_z);
624  FLAGS_SET_M(dims, state->has_m);
625 
626  polygon_total = FindPolygons(obj, &Outer);
627 
628  if (state->config->simple_geometries == 1 && polygon_total != 1) /* We write Non-MULTI geometries, but have several parts: */
629  {
630  snprintf(state->message, SHPLOADERMSGLEN, _("We have a Multipolygon with %d parts, can't use -S switch!"), polygon_total);
631 
632  return SHPLOADERERR;
633  }
634 
635  /* Allocate memory for our array of LWPOLYs */
636  lwpolygons = malloc(sizeof(LWPOLY *) * polygon_total);
637 
638  /* Cycle through each individual polygon */
639  for (pi = 0; pi < polygon_total; pi++)
640  {
641  LWPOLY *lwpoly = lwpoly_construct_empty(state->from_srid, state->has_z, state->has_m);
642 
643  Ring *polyring;
644  int ring_index = 0;
645 
646  /* Firstly count through the total number of rings in this polygon */
647  ring_total = 0;
648  polyring = Outer[pi];
649  while (polyring)
650  {
651  ring_total++;
652  polyring = polyring->next;
653  }
654 
655  /* Cycle through each ring within the polygon, starting with the outer */
656  polyring = Outer[pi];
657 
658  while (polyring)
659  {
660  /* Create a POINTARRAY containing the points making up the ring */
661  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, polyring->n);
662 
663  for (vi = 0; vi < polyring->n; vi++)
664  {
665  /* Build up a point array of all the points in this ring */
666  point4d.x = polyring->list[vi].x;
667  point4d.y = polyring->list[vi].y;
668 
669  if (state->has_z)
670  point4d.z = polyring->list[vi].z;
671  if (state->has_m)
672  point4d.m = polyring->list[vi].m;
673 
674  ptarray_append_point(pa, &point4d, LW_TRUE);
675  }
676 
677  /* Copy the POINTARRAY pointer so we can use the LWPOLY constructor */
678  lwpoly_add_ring(lwpoly, pa);
679 
680  polyring = polyring->next;
681  ring_index++;
682  }
683 
684  /* Generate the LWGEOM */
685  lwpolygons[pi] = lwpoly_as_lwgeom(lwpoly);
686  }
687 
688  /* If using MULTIPOLYGONS then generate the serialized collection, otherwise just a single POLYGON */
689  if (state->config->simple_geometries == 0)
690  {
691  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTIPOLYGONTYPE, state->from_srid, NULL, polygon_total, lwpolygons));
692  }
693  else
694  {
695  lwgeom = lwpolygons[0];
696  lwfree(lwpolygons);
697  }
698 
699  if (!state->config->use_wkt)
700  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
701  else
702  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
703 
704  if ( !mem )
705  {
706  /* Free the linked list of rings */
707  ReleasePolygons(Outer, polygon_total);
708 
709  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
710  return SHPLOADERERR;
711  }
712 
713  /* Free all of the allocated items */
714  lwgeom_free(lwgeom);
715 
716  /* Free the linked list of rings */
717  ReleasePolygons(Outer, polygon_total);
718 
719  /* Return the string - everything ok */
720  *geometry = mem;
721 
722  return SHPLOADEROK;
723 }
724 
725 
726 /*
727  * External functions (defined in shp2pgsql-core.h)
728  */
729 
730 
731 /* Convert the string to lower case */
732 void
733 strtolower(char *s)
734 {
735  int j;
736 
737  for (j = 0; j < strlen(s); j++)
738  s[j] = tolower(s[j]);
739 }
740 
741 
742 /* Default configuration settings */
743 void
745 {
746  config->opt = 'c';
747  config->table = NULL;
748  config->schema = NULL;
749  config->geo_col = NULL;
750  config->shp_file = NULL;
751  config->dump_format = 0;
752  config->simple_geometries = 0;
753  config->geography = 0;
754  config->quoteidentifiers = 0;
755  config->forceint4 = 0;
756  config->createindex = 0;
757  config->readshape = 1;
759  config->encoding = strdup(ENCODING_DEFAULT);
761  config->sr_id = SRID_UNKNOWN;
762  config->shp_sr_id = SRID_UNKNOWN;
763  config->use_wkt = 0;
764  config->tablespace = NULL;
765  config->idxtablespace = NULL;
766  config->usetransaction = 1;
767  config->column_map_filename = NULL;
768 }
769 
770 /* Create a new shapefile state object */
773 {
774  SHPLOADERSTATE *state;
775 
776  /* Create a new state object and assign the config to it */
777  state = malloc(sizeof(SHPLOADERSTATE));
778  state->config = config;
779 
780  /* Set any state defaults */
781  state->hSHPHandle = NULL;
782  state->hDBFHandle = NULL;
783  state->has_z = 0;
784  state->has_m = 0;
785  state->types = NULL;
786  state->widths = NULL;
787  state->precisions = NULL;
788  state->col_names = NULL;
789  state->field_names = NULL;
790 
791  state->from_srid = config->shp_sr_id;
792  state->to_srid = config->sr_id;
793 
794  /* If only one has a valid SRID, use it for both. */
795  if (state->to_srid == SRID_UNKNOWN)
796  {
797  if (config->geography)
798  {
799  state->to_srid = 4326;
800  }
801  else
802  {
803  state->to_srid = state->from_srid;
804  }
805  }
806 
807  if (state->from_srid == SRID_UNKNOWN)
808  {
809  state->from_srid = state->to_srid;
810  }
811 
812  /* If the geo col name is not set, use one of the defaults. */
813  state->geo_col = config->geo_col;
814 
815  if (!state->geo_col)
816  {
817  state->geo_col = strdup(config->geography ? GEOGRAPHY_DEFAULT : GEOMETRY_DEFAULT);
818  }
819 
820  colmap_init(&state->column_map);
821 
822  return state;
823 }
824 
825 
826 /* Open the shapefile and extract the relevant field information */
827 int
829 {
830  SHPObject *obj = NULL;
831  int j, z;
832  int ret = SHPLOADEROK;
833 
834  int field_precision, field_width;
835  char name[MAXFIELDNAMELEN];
836  char name2[MAXFIELDNAMELEN];
837  DBFFieldType type = -1;
838  char *utf8str;
839 
840  /* If we are reading the entire shapefile, open it */
841  if (state->config->readshape == 1)
842  {
843  state->hSHPHandle = SHPOpen(state->config->shp_file, "rb");
844 
845  if (state->hSHPHandle == NULL)
846  {
847  snprintf(state->message, SHPLOADERMSGLEN, _("%s: shape (.shp) or index files (.shx) can not be opened, will just import attribute data."), state->config->shp_file);
848  state->config->readshape = 0;
849 
850  ret = SHPLOADERWARN;
851  }
852  }
853 
854  /* Open the DBF (attributes) file */
855  state->hDBFHandle = DBFOpen(state->config->shp_file, "rb");
856  if ((state->hSHPHandle == NULL && state->config->readshape == 1) || state->hDBFHandle == NULL)
857  {
858  snprintf(state->message, SHPLOADERMSGLEN, _("%s: dbf file (.dbf) can not be opened."), state->config->shp_file);
859 
860  return SHPLOADERERR;
861  }
862 
863 
864  /* Open the column map if one was specified */
865  if (state->config->column_map_filename)
866  {
867  ret = colmap_read(state->config->column_map_filename,
868  &state->column_map, state->message, SHPLOADERMSGLEN);
869  if (!ret) return SHPLOADERERR;
870  }
871 
872  /* User hasn't altered the default encoding preference... */
873  if ( strcmp(state->config->encoding, ENCODING_DEFAULT) == 0 )
874  {
875  /* But the file has a code page entry... */
876  if ( state->hDBFHandle->pszCodePage )
877  {
878  /* And we figured out what iconv encoding it maps to, so use it! */
879  char *newencoding = NULL;
880  if ( (newencoding = codepage2encoding(state->hDBFHandle->pszCodePage)) )
881  {
882  lwfree(state->config->encoding);
883  state->config->encoding = newencoding;
884  }
885  }
886  }
887 
888  /* If reading the whole shapefile (not just attributes)... */
889  if (state->config->readshape == 1)
890  {
891  SHPGetInfo(state->hSHPHandle, &state->num_entities, &state->shpfiletype, NULL, NULL);
892 
893  /* If null_policy is set to abort, check for NULLs */
894  if (state->config->null_policy == POLICY_NULL_ABORT)
895  {
896  /* If we abort on null items, scan the entire file for NULLs */
897  for (j = 0; j < state->num_entities; j++)
898  {
899  obj = SHPReadObject(state->hSHPHandle, j);
900 
901  if (!obj)
902  {
903  snprintf(state->message, SHPLOADERMSGLEN, _("Error reading shape object %d"), j);
904  return SHPLOADERERR;
905  }
906 
907  if (obj->nVertices == 0)
908  {
909  snprintf(state->message, SHPLOADERMSGLEN, _("Empty geometries found, aborted.)"));
910  return SHPLOADERERR;
911  }
912 
913  SHPDestroyObject(obj);
914  }
915  }
916 
917  /* Check the shapefile type */
918  int geomtype = 0;
919  switch (state->shpfiletype)
920  {
921  case SHPT_POINT:
922  /* Point */
923  state->pgtype = "POINT";
924  geomtype = POINTTYPE;
925  state->pgdims = 2;
926  break;
927 
928  case SHPT_ARC:
929  /* PolyLine */
930  state->pgtype = "MULTILINESTRING";
931  geomtype = MULTILINETYPE ;
932  state->pgdims = 2;
933  break;
934 
935  case SHPT_POLYGON:
936  /* Polygon */
937  state->pgtype = "MULTIPOLYGON";
938  geomtype = MULTIPOLYGONTYPE;
939  state->pgdims = 2;
940  break;
941 
942  case SHPT_MULTIPOINT:
943  /* MultiPoint */
944  state->pgtype = "MULTIPOINT";
945  geomtype = MULTIPOINTTYPE;
946  state->pgdims = 2;
947  break;
948 
949  case SHPT_POINTM:
950  /* PointM */
951  geomtype = POINTTYPE;
952  state->has_m = 1;
953  state->pgtype = "POINTM";
954  state->pgdims = 3;
955  break;
956 
957  case SHPT_ARCM:
958  /* PolyLineM */
959  geomtype = MULTILINETYPE;
960  state->has_m = 1;
961  state->pgtype = "MULTILINESTRINGM";
962  state->pgdims = 3;
963  break;
964 
965  case SHPT_POLYGONM:
966  /* PolygonM */
967  geomtype = MULTIPOLYGONTYPE;
968  state->has_m = 1;
969  state->pgtype = "MULTIPOLYGONM";
970  state->pgdims = 3;
971  break;
972 
973  case SHPT_MULTIPOINTM:
974  /* MultiPointM */
975  geomtype = MULTIPOINTTYPE;
976  state->has_m = 1;
977  state->pgtype = "MULTIPOINTM";
978  state->pgdims = 3;
979  break;
980 
981  case SHPT_POINTZ:
982  /* PointZ */
983  geomtype = POINTTYPE;
984  state->has_m = 1;
985  state->has_z = 1;
986  state->pgtype = "POINT";
987  state->pgdims = 4;
988  break;
989 
990  case SHPT_ARCZ:
991  /* PolyLineZ */
992  state->pgtype = "MULTILINESTRING";
993  geomtype = MULTILINETYPE;
994  state->has_z = 1;
995  state->has_m = 1;
996  state->pgdims = 4;
997  break;
998 
999  case SHPT_POLYGONZ:
1000  /* MultiPolygonZ */
1001  state->pgtype = "MULTIPOLYGON";
1002  geomtype = MULTIPOLYGONTYPE;
1003  state->has_z = 1;
1004  state->has_m = 1;
1005  state->pgdims = 4;
1006  break;
1007 
1008  case SHPT_MULTIPOINTZ:
1009  /* MultiPointZ */
1010  state->pgtype = "MULTIPOINT";
1011  geomtype = MULTIPOINTTYPE;
1012  state->has_z = 1;
1013  state->has_m = 1;
1014  state->pgdims = 4;
1015  break;
1016 
1017  default:
1018  state->pgtype = "GEOMETRY";
1019  geomtype = COLLECTIONTYPE;
1020  state->has_z = 1;
1021  state->has_m = 1;
1022  state->pgdims = 4;
1023 
1024  snprintf(state->message, SHPLOADERMSGLEN, _("Unknown geometry type: %d\n"), state->shpfiletype);
1025  return SHPLOADERERR;
1026 
1027  break;
1028  }
1029 
1030  /* Force Z/M-handling if configured to do so */
1031  switch(state->config->force_output)
1032  {
1033  case FORCE_OUTPUT_2D:
1034  state->has_z = 0;
1035  state->has_m = 0;
1036  state->pgdims = 2;
1037  break;
1038 
1039  case FORCE_OUTPUT_3DZ:
1040  state->has_z = 1;
1041  state->has_m = 0;
1042  state->pgdims = 3;
1043  break;
1044 
1045  case FORCE_OUTPUT_3DM:
1046  state->has_z = 0;
1047  state->has_m = 1;
1048  state->pgdims = 3;
1049  break;
1050 
1051  case FORCE_OUTPUT_4D:
1052  state->has_z = 1;
1053  state->has_m = 1;
1054  state->pgdims = 4;
1055  break;
1056  default:
1057  /* Simply use the auto-detected values above */
1058  break;
1059  }
1060 
1061  /* If in simple geometry mode, alter names for CREATE TABLE by skipping MULTI */
1062  if (state->config->simple_geometries)
1063  {
1064  if ((geomtype == MULTIPOLYGONTYPE) || (geomtype == MULTILINETYPE) || (geomtype == MULTIPOINTTYPE))
1065  {
1066  /* Chop off the "MULTI" from the string. */
1067  state->pgtype += 5;
1068  }
1069  }
1070 
1071  }
1072  else
1073  {
1074  /* Otherwise just count the number of records in the DBF */
1075  state->num_entities = DBFGetRecordCount(state->hDBFHandle);
1076  }
1077 
1078 
1079  /* Get the field information from the DBF */
1080  state->num_fields = DBFGetFieldCount(state->hDBFHandle);
1081  state->num_records = DBFGetRecordCount(state->hDBFHandle);
1082 
1083  /* Allocate storage for field information */
1084  state->field_names = malloc(state->num_fields * sizeof(char*));
1085  state->types = (DBFFieldType *)malloc(state->num_fields * sizeof(int));
1086  state->widths = malloc(state->num_fields * sizeof(int));
1087  state->precisions = malloc(state->num_fields * sizeof(int));
1088  state->pgfieldtypes = malloc(state->num_fields * sizeof(char *));
1089  state->col_names = malloc((state->num_fields + 2) * sizeof(char) * MAXFIELDNAMELEN);
1090 
1091  /* Generate a string of comma separated column names of the form "(col1, col2 ... colN)" for the SQL
1092  insertion string */
1093  strcpy(state->col_names, "(" );
1094 
1095  for (j = 0; j < state->num_fields; j++)
1096  {
1097  type = DBFGetFieldInfo(state->hDBFHandle, j, name, &field_width, &field_precision);
1098 
1099  state->types[j] = type;
1100  state->widths[j] = field_width;
1101  state->precisions[j] = field_precision;
1102 
1103  if (state->config->encoding)
1104  {
1105  char *encoding_msg = _("Try \"LATIN1\" (Western European), or one of the values described at http://www.gnu.org/software/libiconv/.");
1106 
1107  int rv = utf8(state->config->encoding, name, &utf8str);
1108 
1109  if (rv != UTF8_GOOD_RESULT)
1110  {
1111  if ( rv == UTF8_BAD_RESULT )
1112  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert field name \"%s\" to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), utf8str, strerror(errno), state->config->encoding, encoding_msg);
1113  else if ( rv == UTF8_NO_RESULT )
1114  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert field name to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), strerror(errno), state->config->encoding, encoding_msg);
1115  else
1116  snprintf(state->message, SHPLOADERMSGLEN, _("Unexpected return value from utf8()"));
1117 
1118  if ( rv == UTF8_BAD_RESULT )
1119  free(utf8str);
1120 
1121  return SHPLOADERERR;
1122  }
1123 
1124  strncpy(name, utf8str, MAXFIELDNAMELEN);
1125  free(utf8str);
1126  }
1127 
1128  /* If a column map file has been passed in, use this to create the postgresql field name from
1129  the dbf column name */
1130  {
1131  const char *mapped = colmap_pg_by_dbf(&state->column_map, name);
1132  if (mapped)
1133  {
1134  strncpy(name, mapped, MAXFIELDNAMELEN);
1135  name[MAXFIELDNAMELEN-1] = '\0';
1136  }
1137  }
1138 
1139  /*
1140  * Make field names lowercase unless asked to
1141  * keep identifiers case.
1142  */
1143  if (!state->config->quoteidentifiers)
1144  strtolower(name);
1145 
1146  /*
1147  * Escape names starting with the
1148  * escape char (_), those named 'gid'
1149  * or after pgsql reserved attribute names
1150  */
1151  if (name[0] == '_' ||
1152  ! strcmp(name, "gid") || ! strcmp(name, "tableoid") ||
1153  ! strcmp(name, "cmin") ||
1154  ! strcmp(name, "cmax") ||
1155  ! strcmp(name, "xmin") ||
1156  ! strcmp(name, "xmax") ||
1157  ! strcmp(name, "primary") ||
1158  ! strcmp(name, "oid") || ! strcmp(name, "ctid"))
1159  {
1160  strncpy(name2 + 2, name, MAXFIELDNAMELEN - 2);
1161  name2[0] = '_';
1162  name2[1] = '_';
1163  strcpy(name, name2);
1164  }
1165 
1166  /* Avoid duplicating field names */
1167  for (z = 0; z < j ; z++)
1168  {
1169  if (strcmp(state->field_names[z], name) == 0)
1170  {
1171  strncat(name, "__", MAXFIELDNAMELEN);
1172  snprintf(name + strlen(name), MAXFIELDNAMELEN, "%i", j);
1173  break;
1174  }
1175  }
1176 
1177  state->field_names[j] = malloc(strlen(name) + 1);
1178  strcpy(state->field_names[j], name);
1179 
1180  /* Now generate the PostgreSQL type name string and width based upon the shapefile type */
1181  switch (state->types[j])
1182  {
1183  case FTString:
1184  state->pgfieldtypes[j] = malloc(strlen("varchar") + 1);
1185  strcpy(state->pgfieldtypes[j], "varchar");
1186  break;
1187 
1188  case FTDate:
1189  state->pgfieldtypes[j] = malloc(strlen("date") + 1);
1190  strcpy(state->pgfieldtypes[j], "date");
1191  break;
1192 
1193  case FTInteger:
1194  /* Determine exact type based upon field width */
1195  if (state->config->forceint4 || (state->widths[j] >=5 && state->widths[j] < 10))
1196  {
1197  state->pgfieldtypes[j] = malloc(strlen("int4") + 1);
1198  strcpy(state->pgfieldtypes[j], "int4");
1199  }
1200  else if (state->widths[j] < 5)
1201  {
1202  state->pgfieldtypes[j] = malloc(strlen("int2") + 1);
1203  strcpy(state->pgfieldtypes[j], "int2");
1204  }
1205  else
1206  {
1207  state->pgfieldtypes[j] = malloc(strlen("numeric") + 1);
1208  strcpy(state->pgfieldtypes[j], "numeric");
1209  }
1210  break;
1211 
1212  case FTDouble:
1213  /* Determine exact type based upon field width */
1214  if (state->widths[j] > 18)
1215  {
1216  state->pgfieldtypes[j] = malloc(strlen("numeric") + 1);
1217  strcpy(state->pgfieldtypes[j], "numeric");
1218  }
1219  else
1220  {
1221  state->pgfieldtypes[j] = malloc(strlen("float8") + 1);
1222  strcpy(state->pgfieldtypes[j], "float8");
1223  }
1224  break;
1225 
1226  case FTLogical:
1227  state->pgfieldtypes[j] = malloc(strlen("boolean") + 1);
1228  strcpy(state->pgfieldtypes[j], "boolean");
1229  break;
1230 
1231  default:
1232  snprintf(state->message, SHPLOADERMSGLEN, _("Invalid type %x in DBF file"), state->types[j]);
1233  return SHPLOADERERR;
1234  }
1235 
1236  strcat(state->col_names, "\"");
1237  strcat(state->col_names, name);
1238 
1239  if (state->config->readshape == 1 || j < (state->num_fields - 1))
1240  {
1241  /* Don't include last comma if its the last field and no geometry field will follow */
1242  strcat(state->col_names, "\",");
1243  }
1244  else
1245  {
1246  strcat(state->col_names, "\"");
1247  }
1248  }
1249 
1250  /* Append the geometry column if required */
1251  if (state->config->readshape == 1)
1252  strcat(state->col_names, state->geo_col);
1253 
1254  strcat(state->col_names, ")");
1255 
1256 
1257  /* Return status */
1258  return ret;
1259 }
1260 
1261 /* Return a pointer to an allocated string containing the header for the specified loader state */
1262 int
1263 ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
1264 {
1265  stringbuffer_t *sb;
1266  char *ret;
1267  int j;
1268 
1269  /* Create the stringbuffer containing the header; we use this API as it's easier
1270  for handling string resizing during append */
1271  sb = stringbuffer_create();
1272  stringbuffer_clear(sb);
1273 
1274  /* Set the client encoding if required */
1275  if (state->config->encoding)
1276  {
1277  stringbuffer_aprintf(sb, "SET CLIENT_ENCODING TO UTF8;\n");
1278  }
1279 
1280  /* Use SQL-standard string escaping rather than PostgreSQL standard */
1281  stringbuffer_aprintf(sb, "SET STANDARD_CONFORMING_STRINGS TO ON;\n");
1282 
1283  /* Drop table if requested */
1284  if (state->config->opt == 'd')
1285  {
1297  if (state->config->schema)
1298  {
1299  if (state->config->readshape == 1 && (! state->config->geography) )
1300  {
1301  stringbuffer_aprintf(sb, "SELECT DropGeometryColumn('%s','%s','%s');\n",
1302  state->config->schema, state->config->table, state->geo_col);
1303  }
1304 
1305  stringbuffer_aprintf(sb, "DROP TABLE \"%s\".\"%s\";\n", state->config->schema,
1306  state->config->table);
1307  }
1308  else
1309  {
1310  if (state->config->readshape == 1 && (! state->config->geography) )
1311  {
1312  stringbuffer_aprintf(sb, "SELECT DropGeometryColumn('','%s','%s');\n",
1313  state->config->table, state->geo_col);
1314  }
1315 
1316  stringbuffer_aprintf(sb, "DROP TABLE \"%s\";\n", state->config->table);
1317  }
1318  }
1319 
1320  /* Start of transaction if we are using one */
1321  if (state->config->usetransaction)
1322  {
1323  stringbuffer_aprintf(sb, "BEGIN;\n");
1324  }
1325 
1326  /* If not in 'append' mode create the spatial table */
1327  if (state->config->opt != 'a')
1328  {
1329  /*
1330  * Create a table for inserting the shapes into with appropriate
1331  * columns and types
1332  */
1333  if (state->config->schema)
1334  {
1335  stringbuffer_aprintf(sb, "CREATE TABLE \"%s\".\"%s\" (gid serial",
1336  state->config->schema, state->config->table);
1337  }
1338  else
1339  {
1340  stringbuffer_aprintf(sb, "CREATE TABLE \"%s\" (gid serial", state->config->table);
1341  }
1342 
1343  /* Generate the field types based upon the shapefile information */
1344  for (j = 0; j < state->num_fields; j++)
1345  {
1346  stringbuffer_aprintf(sb, ",\n\"%s\" ", state->field_names[j]);
1347 
1348  /* First output the raw field type string */
1349  stringbuffer_aprintf(sb, "%s", state->pgfieldtypes[j]);
1350 
1351  /* Some types do have typmods though... */
1352  if (!strcmp("varchar", state->pgfieldtypes[j]))
1353  stringbuffer_aprintf(sb, "(%d)", state->widths[j]);
1354 
1355  if (!strcmp("numeric", state->pgfieldtypes[j]))
1356  {
1357  /* Doubles we just allow PostgreSQL to auto-detect the size */
1358  if (state->types[j] != FTDouble)
1359  stringbuffer_aprintf(sb, "(%d,0)", state->widths[j]);
1360  }
1361  }
1362 
1363  /* Add the geography column directly to the table definition, we don't
1364  need to do an AddGeometryColumn() call. */
1365  if (state->config->readshape == 1 && state->config->geography)
1366  {
1367  char *dimschar;
1368 
1369  if (state->pgdims == 4)
1370  dimschar = "ZM";
1371  else
1372  dimschar = "";
1373 
1374  if (state->to_srid != SRID_UNKNOWN && state->to_srid != 4326)
1375  {
1376  snprintf(state->message, SHPLOADERMSGLEN, _("Invalid SRID for geography type: %d"), state->to_srid);
1378  return SHPLOADERERR;
1379  }
1380  stringbuffer_aprintf(sb, ",\n\"%s\" geography(%s%s,%d)", state->geo_col, state->pgtype, dimschar, 4326);
1381  }
1382  stringbuffer_aprintf(sb, ")");
1383 
1384  /* Tablespace is optional. */
1385  if (state->config->tablespace != NULL)
1386  {
1387  stringbuffer_aprintf(sb, " TABLESPACE \"%s\"", state->config->tablespace);
1388  }
1389  stringbuffer_aprintf(sb, ";\n");
1390 
1391  /* Create the primary key. This is done separately because the index for the PK needs
1392  * to be in the correct tablespace. */
1393 
1394  /* TODO: Currently PostgreSQL does not allow specifying an index to use for a PK (so you get
1395  * a default one called table_pkey) and it does not provide a way to create a PK index
1396  * in a specific tablespace. So as a hacky solution we create the PK, then move the
1397  * index to the correct tablespace. Eventually this should be:
1398  * CREATE INDEX table_pkey on table(gid) TABLESPACE tblspc;
1399  * ALTER TABLE table ADD PRIMARY KEY (gid) USING INDEX table_pkey;
1400  * A patch has apparently been submitted to PostgreSQL to enable this syntax, see this thread:
1401  * http://archives.postgresql.org/pgsql-hackers/2011-01/msg01405.php */
1402  stringbuffer_aprintf(sb, "ALTER TABLE ");
1403 
1404  /* Schema is optional, include if present. */
1405  if (state->config->schema)
1406  {
1407  stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
1408  }
1409  stringbuffer_aprintf(sb, "\"%s\" ADD PRIMARY KEY (gid);\n", state->config->table);
1410 
1411  /* Tablespace is optional for the index. */
1412  if (state->config->idxtablespace != NULL)
1413  {
1414  stringbuffer_aprintf(sb, "ALTER INDEX ");
1415  if (state->config->schema)
1416  {
1417  stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
1418  }
1419 
1420  /* WARNING: We're assuming the default "table_pkey" name for the primary
1421  * key index. PostgreSQL may use "table_pkey1" or similar in the
1422  * case of a name conflict, so you may need to edit the produced
1423  * SQL in this rare case. */
1424  stringbuffer_aprintf(sb, "\"%s_pkey\" SET TABLESPACE \"%s\";\n",
1425  state->config->table, state->config->idxtablespace);
1426  }
1427 
1428  /* Create the geometry column with an addgeometry call */
1429  if (state->config->readshape == 1 && (!state->config->geography))
1430  {
1431  /* If they didn't specify a target SRID, see if they specified a source SRID. */
1432  int srid = state->to_srid;
1433  if (state->config->schema)
1434  {
1435  stringbuffer_aprintf(sb, "SELECT AddGeometryColumn('%s','%s','%s','%d',",
1436  state->config->schema, state->config->table, state->geo_col, srid);
1437  }
1438  else
1439  {
1440  stringbuffer_aprintf(sb, "SELECT AddGeometryColumn('','%s','%s','%d',",
1441  state->config->table, state->geo_col, srid);
1442  }
1443 
1444  stringbuffer_aprintf(sb, "'%s',%d);\n", state->pgtype, state->pgdims);
1445  }
1446  }
1447 
1448  /* Copy the string buffer into a new string, destroying the string buffer */
1449  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1450  strcpy(ret, (char *)stringbuffer_getstring(sb));
1452 
1453  *strheader = ret;
1454 
1455  return SHPLOADEROK;
1456 }
1457 
1458 
1459 /* Return an allocated string containing the copy statement for this state */
1460 int
1462 {
1463  char *copystr;
1464 
1465  /* Allocate the string for the COPY statement */
1466  if (state->config->dump_format)
1467  {
1468  if (state->config->schema)
1469  {
1470  copystr = malloc(strlen(state->config->schema) + strlen(state->config->table) +
1471  strlen(state->col_names) + 40);
1472 
1473  sprintf(copystr, "COPY \"%s\".\"%s\" %s FROM stdin;\n",
1474  state->config->schema, state->config->table, state->col_names);
1475  }
1476  else
1477  {
1478  copystr = malloc(strlen(state->config->table) + strlen(state->col_names) + 40);
1479 
1480  sprintf(copystr, "COPY \"%s\" %s FROM stdin;\n", state->config->table, state->col_names);
1481  }
1482 
1483  *strheader = copystr;
1484  return SHPLOADEROK;
1485  }
1486  else
1487  {
1488  /* Flag an error as something has gone horribly wrong */
1489  snprintf(state->message, SHPLOADERMSGLEN, _("Internal error: attempt to generate a COPY statement for data that hasn't been requested in COPY format"));
1490 
1491  return SHPLOADERERR;
1492  }
1493 }
1494 
1495 
1496 /* Return a count of the number of entities in this shapefile */
1497 int
1499 {
1500  return state->num_entities;
1501 }
1502 
1503 
1504 /* Return an allocated string representation of a specified record item */
1505 int
1506 ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
1507 {
1508  SHPObject *obj = NULL;
1509  stringbuffer_t *sb;
1510  stringbuffer_t *sbwarn;
1511  char val[MAXVALUELEN];
1512  char *escval;
1513  char *geometry=NULL, *ret;
1514  char *utf8str;
1515  int res, i;
1516  int rv;
1517 
1518  /* Clear the stringbuffers */
1519  sbwarn = stringbuffer_create();
1520  stringbuffer_clear(sbwarn);
1521  sb = stringbuffer_create();
1522  stringbuffer_clear(sb);
1523 
1524  /* If we are reading the DBF only and the record has been marked deleted, return deleted record status */
1525  if (state->config->readshape == 0 && DBFIsRecordDeleted(state->hDBFHandle, item))
1526  {
1527  *strrecord = NULL;
1528  return SHPLOADERRECDELETED;
1529  }
1530 
1531  /* If we are reading the shapefile, open the specified record */
1532  if (state->config->readshape == 1)
1533  {
1534  obj = SHPReadObject(state->hSHPHandle, item);
1535  if (!obj)
1536  {
1537  snprintf(state->message, SHPLOADERMSGLEN, _("Error reading shape object %d"), item);
1538  return SHPLOADERERR;
1539  }
1540 
1541  /* If we are set to skip NULLs, return a NULL record status */
1542  if (state->config->null_policy == POLICY_NULL_SKIP && obj->nVertices == 0 )
1543  {
1544  SHPDestroyObject(obj);
1545 
1546  *strrecord = NULL;
1547  return SHPLOADERRECISNULL;
1548  }
1549  }
1550 
1551  /* If not in dump format, generate the INSERT string */
1552  if (!state->config->dump_format)
1553  {
1554  if (state->config->schema)
1555  {
1556  stringbuffer_aprintf(sb, "INSERT INTO \"%s\".\"%s\" %s VALUES (", state->config->schema,
1557  state->config->table, state->col_names);
1558  }
1559  else
1560  {
1561  stringbuffer_aprintf(sb, "INSERT INTO \"%s\" %s VALUES (", state->config->table,
1562  state->col_names);
1563  }
1564  }
1565 
1566 
1567  /* Read all of the attributes from the DBF file for this item */
1568  for (i = 0; i < DBFGetFieldCount(state->hDBFHandle); i++)
1569  {
1570  /* Special case for NULL attributes */
1571  if (DBFIsAttributeNULL(state->hDBFHandle, item, i))
1572  {
1573  if (state->config->dump_format)
1574  stringbuffer_aprintf(sb, "\\N");
1575  else
1576  stringbuffer_aprintf(sb, "NULL");
1577  }
1578  else
1579  {
1580  /* Attribute NOT NULL */
1581  switch (state->types[i])
1582  {
1583  case FTInteger:
1584  case FTDouble:
1585  rv = snprintf(val, MAXVALUELEN, "%s", DBFReadStringAttribute(state->hDBFHandle, item, i));
1586  if (rv >= MAXVALUELEN || rv == -1)
1587  {
1588  stringbuffer_aprintf(sbwarn, "Warning: field %d name truncated\n", i);
1589  val[MAXVALUELEN - 1] = '\0';
1590  }
1591 
1592  /* If the value is an empty string, change to 0 */
1593  if (val[0] == '\0')
1594  {
1595  val[0] = '0';
1596  val[1] = '\0';
1597  }
1598 
1599  /* If the value ends with just ".", remove the dot */
1600  if (val[strlen(val) - 1] == '.')
1601  val[strlen(val) - 1] = '\0';
1602  break;
1603 
1604  case FTString:
1605  case FTLogical:
1606  case FTDate:
1607  rv = snprintf(val, MAXVALUELEN, "%s", DBFReadStringAttribute(state->hDBFHandle, item, i));
1608  if (rv >= MAXVALUELEN || rv == -1)
1609  {
1610  stringbuffer_aprintf(sbwarn, "Warning: field %d name truncated\n", i);
1611  val[MAXVALUELEN - 1] = '\0';
1612  }
1613  break;
1614 
1615  default:
1616  snprintf(state->message, SHPLOADERMSGLEN, _("Error: field %d has invalid or unknown field type (%d)"), i, state->types[i]);
1617 
1618  /* clean up and return err */
1619  SHPDestroyObject(obj);
1620  stringbuffer_destroy(sbwarn);
1622  return SHPLOADERERR;
1623  }
1624 
1625  if (state->config->encoding)
1626  {
1627  char *encoding_msg = _("Try \"LATIN1\" (Western European), or one of the values described at http://www.postgresql.org/docs/current/static/multibyte.html.");
1628 
1629  rv = utf8(state->config->encoding, val, &utf8str);
1630 
1631  if (rv != UTF8_GOOD_RESULT)
1632  {
1633  if ( rv == UTF8_BAD_RESULT )
1634  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert data value \"%s\" to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), utf8str, strerror(errno), state->config->encoding, encoding_msg);
1635  else if ( rv == UTF8_NO_RESULT )
1636  snprintf(state->message, SHPLOADERMSGLEN, _("Unable to convert data value to UTF-8 (iconv reports \"%s\"). Current encoding is \"%s\". %s"), strerror(errno), state->config->encoding, encoding_msg);
1637  else
1638  snprintf(state->message, SHPLOADERMSGLEN, _("Unexpected return value from utf8()"));
1639 
1640  if ( rv == UTF8_BAD_RESULT )
1641  free(utf8str);
1642 
1643  /* clean up and return err */
1644  SHPDestroyObject(obj);
1645  stringbuffer_destroy(sbwarn);
1647  return SHPLOADERERR;
1648  }
1649  strncpy(val, utf8str, MAXVALUELEN);
1650  free(utf8str);
1651 
1652  }
1653 
1654  /* Escape attribute correctly according to dump format */
1655  if (state->config->dump_format)
1656  {
1657  escval = escape_copy_string(val);
1658  stringbuffer_aprintf(sb, "%s", escval);
1659  }
1660  else
1661  {
1662  escval = escape_insert_string(val);
1663  stringbuffer_aprintf(sb, "'%s'", escval);
1664  }
1665 
1666  /* Free the escaped version if required */
1667  if (val != escval)
1668  free(escval);
1669  }
1670 
1671  /* Only put in delimeter if not last field or a shape will follow */
1672  if (state->config->readshape == 1 || i < DBFGetFieldCount(state->hDBFHandle) - 1)
1673  {
1674  if (state->config->dump_format)
1675  stringbuffer_aprintf(sb, "\t");
1676  else
1677  stringbuffer_aprintf(sb, ",");
1678  }
1679 
1680  /* End of DBF attribute loop */
1681  }
1682 
1683 
1684  /* Add the shape attribute if we are reading it */
1685  if (state->config->readshape == 1)
1686  {
1687  /* Force the locale to C */
1688  char *oldlocale = setlocale(LC_NUMERIC, "C");
1689 
1690  /* Handle the case of a NULL shape */
1691  if (obj->nVertices == 0)
1692  {
1693  if (state->config->dump_format)
1694  stringbuffer_aprintf(sb, "\\N");
1695  else
1696  stringbuffer_aprintf(sb, "NULL");
1697  }
1698  else
1699  {
1700  /* Handle all other shape attributes */
1701  switch (obj->nSHPType)
1702  {
1703  case SHPT_POLYGON:
1704  case SHPT_POLYGONM:
1705  case SHPT_POLYGONZ:
1706  res = GeneratePolygonGeometry(state, obj, &geometry);
1707  break;
1708 
1709  case SHPT_POINT:
1710  case SHPT_POINTM:
1711  case SHPT_POINTZ:
1712  res = GeneratePointGeometry(state, obj, &geometry, 0);
1713  break;
1714 
1715  case SHPT_MULTIPOINT:
1716  case SHPT_MULTIPOINTM:
1717  case SHPT_MULTIPOINTZ:
1718  /* Force it to multi unless using -S */
1719  res = GeneratePointGeometry(state, obj, &geometry,
1720  state->config->simple_geometries ? 0 : 1);
1721  break;
1722 
1723  case SHPT_ARC:
1724  case SHPT_ARCM:
1725  case SHPT_ARCZ:
1726  res = GenerateLineStringGeometry(state, obj, &geometry);
1727  break;
1728 
1729  default:
1730  snprintf(state->message, SHPLOADERMSGLEN, _("Shape type is not supported, type id = %d"), obj->nSHPType);
1731  SHPDestroyObject(obj);
1732  stringbuffer_destroy(sbwarn);
1734 
1735  return SHPLOADERERR;
1736  }
1737  /* The default returns out of the function, so res will always have been set. */
1738  if (res != SHPLOADEROK)
1739  {
1740  /* Error message has already been set */
1741  SHPDestroyObject(obj);
1742  stringbuffer_destroy(sbwarn);
1744 
1745  return SHPLOADERERR;
1746  }
1747 
1748  /* Now generate the geometry string according to the current configuration */
1749  if (!state->config->dump_format)
1750  {
1751  if (state->to_srid != state->from_srid)
1752  {
1753  stringbuffer_aprintf(sb, "ST_Transform(");
1754  }
1755  stringbuffer_aprintf(sb, "'");
1756  }
1757 
1758  stringbuffer_aprintf(sb, "%s", geometry);
1759 
1760  if (!state->config->dump_format)
1761  {
1762  stringbuffer_aprintf(sb, "'");
1763 
1764  /* Close the ST_Transform if reprojecting. */
1765  if (state->to_srid != state->from_srid)
1766  {
1767  /* We need to add an explicit cast to geography/geometry to ensure that
1768  PostgreSQL doesn't get confused with the ST_Transform() raster
1769  function. */
1770  if (state->config->geography)
1771  stringbuffer_aprintf(sb, "::geometry, %d)::geography", state->to_srid);
1772  else
1773  stringbuffer_aprintf(sb, "::geometry, %d)", state->to_srid);
1774  }
1775  }
1776 
1777  free(geometry);
1778  }
1779 
1780  /* Tidy up everything */
1781  SHPDestroyObject(obj);
1782 
1783  setlocale(LC_NUMERIC, oldlocale);
1784  }
1785 
1786  /* Close the line correctly for dump/insert format */
1787  if (!state->config->dump_format)
1788  stringbuffer_aprintf(sb, ");");
1789 
1790 
1791  /* Copy the string buffer into a new string, destroying the string buffer */
1792  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1793  strcpy(ret, (char *)stringbuffer_getstring(sb));
1795 
1796  *strrecord = ret;
1797 
1798  /* If any warnings occurred, set the returned message string and warning status */
1799  if (strlen((char *)stringbuffer_getstring(sbwarn)) > 0)
1800  {
1801  snprintf(state->message, SHPLOADERMSGLEN, "%s", stringbuffer_getstring(sbwarn));
1802  stringbuffer_destroy(sbwarn);
1803 
1804  return SHPLOADERWARN;
1805  }
1806  else
1807  {
1808  /* Everything went okay */
1809  stringbuffer_destroy(sbwarn);
1810 
1811  return SHPLOADEROK;
1812  }
1813 }
1814 
1815 
1816 /* Return a pointer to an allocated string containing the header for the specified loader state */
1817 int
1818 ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
1819 {
1820  stringbuffer_t *sb;
1821  char *ret;
1822 
1823  /* Create the stringbuffer containing the header; we use this API as it's easier
1824  for handling string resizing during append */
1825  sb = stringbuffer_create();
1826  stringbuffer_clear(sb);
1827 
1828  /* Create gist index if specified and not in "prepare" mode */
1829  if (state->config->readshape && state->config->createindex)
1830  {
1831  stringbuffer_aprintf(sb, "CREATE INDEX ON ");
1832  /* Schema is optional, include if present. */
1833  if (state->config->schema)
1834  {
1835  stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
1836  }
1837  stringbuffer_aprintf(sb, "\"%s\" USING GIST (\"%s\")", state->config->table, state->geo_col);
1838  /* Tablespace is also optional. */
1839  if (state->config->idxtablespace != NULL)
1840  {
1841  stringbuffer_aprintf(sb, " TABLESPACE \"%s\"", state->config->idxtablespace);
1842  }
1843  stringbuffer_aprintf(sb, ";\n");
1844  }
1845 
1846  /* End the transaction if there is one. */
1847  if (state->config->usetransaction)
1848  {
1849  stringbuffer_aprintf(sb, "COMMIT;\n");
1850  }
1851 
1852  /* Always ANALYZE the resulting table, for better stats */
1853  stringbuffer_aprintf(sb, "ANALYZE ");
1854  if (state->config->schema)
1855  {
1856  stringbuffer_aprintf(sb, "\"%s\".", state->config->schema);
1857  }
1858  stringbuffer_aprintf(sb, "\"%s\";\n", state->config->table);
1859 
1860  /* Copy the string buffer into a new string, destroying the string buffer */
1861  ret = (char *)malloc(strlen((char *)stringbuffer_getstring(sb)) + 1);
1862  strcpy(ret, (char *)stringbuffer_getstring(sb));
1864 
1865  *strfooter = ret;
1866 
1867  return SHPLOADEROK;
1868 }
1869 
1870 
1871 void
1873 {
1874  /* Destroy a state object created with ShpLoaderOpenShape */
1875  int i;
1876 
1877  if (state != NULL)
1878  {
1879  if (state->hSHPHandle)
1880  SHPClose(state->hSHPHandle);
1881  if (state->hDBFHandle)
1882  DBFClose(state->hDBFHandle);
1883  if (state->field_names)
1884  {
1885  for (i = 0; i < state->num_fields; i++)
1886  free(state->field_names[i]);
1887 
1888  free(state->field_names);
1889  }
1890  if (state->pgfieldtypes)
1891  {
1892  for (i = 0; i < state->num_fields; i++)
1893  free(state->pgfieldtypes[i]);
1894 
1895  free(state->pgfieldtypes);
1896  }
1897  if (state->types)
1898  free(state->types);
1899  if (state->widths)
1900  free(state->widths);
1901  if (state->precisions)
1902  free(state->precisions);
1903  if (state->col_names)
1904  free(state->col_names);
1905 
1906  /* Free any column map fieldnames if specified */
1907  colmap_clean(&state->column_map);
1908 
1909  /* Free the state itself */
1910  free(state);
1911  }
1912 }
double x
Definition: liblwgeom.h:336
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:834
void colmap_clean(colmap *map)
Definition: shpcommon.c:87
SHPLOADERCONFIG * config
int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
#define SHPT_ARCM
Definition: shapefil.h:316
tuple res
Definition: window.py:78
DBFFieldType * types
int * panPartStart
Definition: shapefil.h:345
double m
Definition: liblwgeom.h:336
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:46
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:655
void lwfree(void *mem)
Definition: lwutil.c:214
#define FORCE_OUTPUT_3DM
#define SHPT_POLYGONM
Definition: shapefil.h:317
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
Definition: shpcommon.c:131
#define FORCE_OUTPUT_3DZ
int PIP(Point P, Point *V, int n)
PIP(): crossing number test for a point in a polygon input: P = a point, V[] = vertex points of a pol...
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
int nVertices
Definition: shapefil.h:348
#define POLICY_NULL_INSERT
#define SHPLOADERRECISNULL
Datum area(PG_FUNCTION_ARGS)
#define _(String)
Definition: shpcommon.h:24
const char SHPAPI_CALL1 * DBFReadStringAttribute(DBFHandle psDBF, int iRecord, int iField){ return((const char *) DBFReadAttribute(psDBF, iRecord, iField, 'C')
char * codepage2encoding(const char *cpg)
Definition: shpcommon.c:206
#define UTF8_BAD_RESULT
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
#define SHPLOADERWARN
double * padfX
Definition: shapefil.h:349
#define SHPT_MULTIPOINT
Definition: shapefil.h:310
#define POLICY_NULL_SKIP
#define SHPLOADERRECDELETED
#define SHPT_POLYGON
Definition: shapefil.h:309
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
Definition: shpopen.c:2182
#define SHPT_MULTIPOINTZ
Definition: shapefil.h:314
int GeneratePointGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry, int force_multi)
Generate an allocated geometry string for shapefile object obj using the state parameters if "force_m...
int GeneratePolygonGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
int GenerateLineStringGeometry(SHPLOADERSTATE *state, SHPObject *obj, char **geometry)
Generate an allocated geometry string for shapefile object obj using the state parameters.
int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
struct struct_ring Ring
double * padfY
Definition: shapefil.h:350
DBFHandle hDBFHandle
#define UTF8_GOOD_RESULT
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)
Definition: dbfopen.c:1204
#define SHPT_ARCZ
Definition: shapefil.h:312
void ShpLoaderDestroy(SHPLOADERSTATE *state)
#define GEOGRAPHY_DEFAULT
void SHPAPI_CALL SHPGetInfo(SHPHandle hSHP, int *pnEntities, int *pnShapeType, double *padfMinBound, double *padfMaxBound)
Definition: shpopen.c:796
#define POLICY_NULL_ABORT
char * escape_insert_string(char *str)
Escape input string suitable for INSERT.
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided...
Definition: stringbuffer.c:246
void colmap_init(colmap *map)
Definition: shpcommon.c:79
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile, const char *pszAccess)
Definition: shpopen.c:464
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
Definition: dbfopen.c:1217
#define MAXFIELDNAMELEN
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:130
int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF)
Definition: dbfopen.c:1191
DBFHandle SHPAPI_CALL DBFOpen(const char *pszFilename, const char *pszAccess)
Definition: dbfopen.c:365
void set_loader_config_defaults(SHPLOADERCONFIG *config)
#define GEOMETRY_DEFAULT
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
#define FORCE_OUTPUT_2D
int ShpLoaderGetRecordCount(SHPLOADERSTATE *state)
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:156
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:83
#define LW_FALSE
Definition: liblwgeom.h:62
int FindPolygons(SHPObject *obj, Ring ***Out)
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
struct struct_point Point
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
char * escape_copy_string(char *str)
Escape input string suitable for COPY.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
struct struct_ring * next
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
int nParts
Definition: shapefil.h:344
char * s
Definition: cu_in_wkt.c:23
#define SHPLOADEROK
double z
Definition: liblwgeom.h:336
#define WKB_EXTENDED
Definition: liblwgeom.h:1932
#define UTF8_NO_RESULT
#define WKT_PRECISION
#define SHPT_MULTIPOINTM
Definition: shapefil.h:318
#define FORCE_OUTPUT_DISABLE
#define ENCODING_DEFAULT
#define FORCE_OUTPUT_4D
#define SHPT_POINTZ
Definition: shapefil.h:311
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
SHPHandle hSHPHandle
Point * list
#define SHPT_POINTM
Definition: shapefil.h:315
#define WKT_EXTENDED
Definition: liblwgeom.h:1941
#define SHPT_POLYGONZ
Definition: shapefil.h:313
char message[SHPLOADERMSGLEN]
void ReleasePolygons(Ring **polys, int npolys)
#define SHPT_POINT
Definition: shapefil.h:307
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:71
static int utf8(const char *fromcode, char *inputbuf, char **outputbuf)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:66
void SHPAPI_CALL DBFClose(DBFHandle psDBF)
Definition: dbfopen.c:578
double * padfZ
Definition: shapefil.h:351
const char * colmap_pg_by_dbf(colmap *map, const char *dbfname)
Definition: shpcommon.c:117
type
Definition: ovdump.py:41
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:142
#define MAXVALUELEN
void free(void *)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader)
void * malloc(YYSIZE_T)
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring, allocating extra space if necessary.
Definition: lwpoly.c:154
int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle psDBF, int iRecord, int iField)
Definition: dbfopen.c:1171
void strtolower(char *s)
void SHPAPI_CALL SHPClose(SHPHandle hSHP)
Definition: shpopen.c:759
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:98
#define SHPLOADERERR
int nSHPType
Definition: shapefil.h:340
double y
Definition: liblwgeom.h:336
unsigned int linked
#define MULTILINETYPE
Definition: liblwgeom.h:74
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define SHPT_ARC
Definition: shapefil.h:308
double * padfM
Definition: shapefil.h:352
#define SHPLOADERMSGLEN
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
int SHPAPI_CALL DBFIsRecordDeleted(DBFHandle psDBF, int iShape)
Definition: dbfopen.c:1728
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:131
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:219