16 #include "../postgis_config.h"
19 #include "../liblwgeom/liblwgeom.h"
20 #include "../liblwgeom/lwgeom_log.h"
43 #define UTF8_GOOD_RESULT 0
44 #define UTF8_BAD_RESULT 1
45 #define UTF8_NO_RESULT 2
60 utf8(
const char *fromcode,
char *inputbuf,
char **outputbuf)
67 inbytesleft = strlen(inputbuf);
69 cd = iconv_open(
"UTF-8", fromcode);
70 if ( cd == ((iconv_t)(-1)) )
73 outbytesleft = inbytesleft * 3 + 1;
75 *outputbuf = (
char *)
malloc(outbytesleft);
79 memset(*outputbuf, 0, outbytesleft);
80 outputptr = *outputbuf;
83 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == (
size_t)-1 )
88 iconvctl(cd, ICONV_SET_TRANSLITERATE, &on);
89 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
92 iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &on);
93 if ( iconv(cd, &inputbuf, &inbytesleft, &outputptr, &outbytesleft) == -1 )
140 if (*ptr ==
'\t' || *ptr ==
'\\' || *ptr ==
'\n' || *ptr ==
'\r')
150 size = ptr - str + toescape + 1;
151 result = calloc(1, size);
157 if ( *ptr ==
'\t' || *ptr ==
'\\' || *ptr ==
'\n' || *ptr ==
'\r' )
203 size = ptr - str + toescape + 1;
204 result = calloc(1, size);
261 point4d.
x = obj->
padfX[u];
262 point4d.
y = obj->
padfY[u];
265 point4d.
z = obj->
padfZ[u];
267 point4d.
m = obj->
padfM[u];
284 lwgeom = lwmultipoints[0];
321 LWGEOM **lwmultilinestrings;
325 int u, v, start_vertex, end_vertex;
344 for (u = 0; u < obj->
nParts; u++)
358 for (v = start_vertex; v < end_vertex; v++)
361 point4d.
x = obj->
padfX[v];
362 point4d.
y = obj->
padfY[v];
365 point4d.
z = obj->
padfZ[v];
367 point4d.
m = obj->
padfM[v];
383 lwgeom = lwmultilinestrings[0];
384 lwfree(lwmultilinestrings);
421 for (i = 0; i <
n-1; i++)
423 if (((V[i].
y <= P.
y) && (V[i + 1].
y > P.
y))
424 || ((V[i].
y > P.
y) && (V[i + 1].
y <= P.
y)))
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))
445 #if POSTGIS_DEBUG_LEVEL > 0
446 static int call = -1;
450 LWDEBUGF(4,
"FindPolygons[%d]: allocated space for %d rings\n", call, obj->
nParts);
457 for (pi=0; pi < obj->
nParts; pi++)
467 if (pi == obj->
nParts - 1)
485 for (vi = vs; vi < ve; vi++)
507 if (area < 0.0 || obj->nParts == 1)
509 Outer[out_index] = ring;
515 Inner[in_index] = ring;
520 LWDEBUGF(4,
"FindPolygons[%d]: found %d Outer, %d Inners\n", call, out_index, in_index);
524 for (pi = 0; pi < in_index; pi++)
528 Ring *inner = Inner[pi], *outer = NULL;
543 for (i = out_index - 1; i >= 0; i--)
547 in =
PIP(pt, Outer[i]->
list, Outer[i]->
n);
548 if ( in ||
PIP(pt2, Outer[i]->
list, Outer[i]->
n) )
567 LWDEBUGF(4,
"FindPolygons[%d]: hole %d is orphan\n", call, pi);
569 Outer[out_index] = inner;
591 for (pi = 0; pi < npolys; pi++)
619 int polygon_total, ring_total;
639 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"We have a Multipolygon with %d parts, can't use -S switch!"), polygon_total);
648 for (pi = 0; pi < polygon_total; pi++)
657 polyring = Outer[pi];
661 polyring = polyring->
next;
665 polyring = Outer[pi];
672 for (vi = 0; vi < polyring->
n; vi++)
675 point4d.
x = polyring->
list[vi].
x;
676 point4d.
y = polyring->
list[vi].
y;
679 point4d.
z = polyring->
list[vi].
z;
681 point4d.
m = polyring->
list[vi].
m;
689 polyring = polyring->
next;
704 lwgeom = lwpolygons[0];
746 for (j = 0; j < strlen(
s); j++)
747 s[j] = tolower(
s[j]);
756 config->
table = NULL;
845 int field_precision, field_width;
848 DBFFieldType
type = -1;
890 char *newencoding = NULL;
941 state->
pgtype =
"MULTILINESTRING";
948 state->
pgtype =
"MULTIPOLYGON";
955 state->
pgtype =
"MULTIPOINT";
972 state->
pgtype =
"MULTILINESTRINGM";
980 state->
pgtype =
"MULTIPOLYGONM";
988 state->
pgtype =
"MULTIPOINTM";
1003 state->
pgtype =
"MULTILINESTRING";
1012 state->
pgtype =
"MULTIPOLYGON";
1021 state->
pgtype =
"MULTIPOINT";
1029 state->
pgtype =
"GEOMETRY";
1112 state->
widths[j] = field_width;
1118 char *encoding_msg =
_(
"Try \"LATIN1\" (Western European), or one of the values described at http://www.gnu.org/software/libiconv/.");
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);
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);
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"))
1174 size_t len = strlen(name);
1177 strncpy(name2 + 2, name, len);
1179 name2[len + 2] =
'\0';
1182 strcpy(name, name2);
1186 for (z = 0; z < j ; z++)
1191 snprintf(name + strlen(name),
1202 switch (state->
types[j])
1218 else if (state->
widths[j] >=10 && state->
widths[j] < 19)
1222 else if (state->
widths[j] < 5)
1234 fprintf(stderr,
"Field %s is an FTDouble with width %d and precision %d\n",
1236 if (state->
widths[j] > 18)
1378 if (state->
types[j] != FTDouble)
1493 sprintf(copystr,
"COPY \"%s\".\"%s\" %s FROM stdin;\n",
1503 *strheader = copystr;
1509 snprintf(state->
message,
SHPLOADERMSGLEN,
_(
"Internal error: attempt to generate a COPY statement for data that hasn't been requested in COPY format"));
1533 char *geometry=NULL, *ret;
1601 switch (state->
types[i])
1620 if (val[strlen(val) - 1] ==
'.')
1621 val[strlen(val) - 1] =
'\0';
1647 char *encoding_msg =
_(
"Try \"LATIN1\" (Western European), or one of the values described at http://www.postgresql.org/docs/current/static/multibyte.html.");
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);
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);
1709 char *oldlocale = setlocale(LC_NUMERIC,
"C");
1804 setlocale(LC_NUMERIC, oldlocale);
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)
DBFHandle SHPAPI_CALL DBFOpen(const char *pszFilename, const char *pszAccess)
int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF)
void SHPAPI_CALL DBFClose(DBFHandle psDBF)
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle psDBF, int iRecord, int iField)
int SHPAPI_CALL DBFIsRecordDeleted(DBFHandle psDBF, int iShape)
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring, allocating extra space if necessary.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
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,...
#define LW_TRUE
Return types for functions with status returns.
#define FLAGS_SET_M(flags, value)
#define SRID_UNKNOWN
Unknown SRID value.
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
#define FLAGS_SET_Z(flags, value)
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Datum area(PG_FUNCTION_ARGS)
#define LWDEBUGF(level, msg,...)
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
void SHPAPI_CALL SHPClose(SHPHandle hSHP)
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile, const char *pszAccess)
void SHPAPI_CALL SHPGetInfo(SHPHandle hSHP, int *pnEntities, int *pnShapeType, double *padfMinBound, double *padfMaxBound)
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...
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.
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 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)
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 POLICY_NULL_ABORT
#define GEOGRAPHY_DEFAULT
#define SHPLOADERRECISNULL
#define POLICY_NULL_INSERT
#define FORCE_OUTPUT_DISABLE
#define SHPLOADERRECDELETED
int colmap_read(const char *filename, colmap *map, char *errbuf, size_t errbuflen)
Read the content of filename into a symbol map.
void colmap_init(colmap *map)
const char * colmap_pg_by_dbf(colmap *map, const char *dbfname)
void colmap_clean(colmap *map)
char * codepage2encoding(const char *cpg)
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
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.
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
char * column_map_filename
char message[SHPLOADERMSGLEN]
struct struct_ring * next