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