802
806
fprintf(out, " -I Create a GiST index on the geometry column.\n");
803
807
fprintf(out, " -S Generate simple geometries instead of MULTI geometries.\n");
804
808
fprintf(out, " -w Use wkt format (for postgis-0.x support - drops M - drifts coordinates).\n");
806
810
fprintf(out, " -W <encoding> Specify the character encoding of Shape's\n");
807
811
fprintf(out, " attribute column. (default : \"ASCII\")\n");
809
813
fprintf(out, " -N <policy> Specify NULL geometries handling policy (insert,skip,abort)\n");
810
814
fprintf(out, " -n Only import DBF file.\n");
811
fprintf(out, " -? Display this help screen\n");
815
fprintf(out, " -? Display this help screen\n");
816
InsertLineString(int id)
818
int pi; /* part index */
819
unsigned int subtype = LINETYPE | (wkbtype&WKBZOFFSET) |
820
(wkbtype&WKBMOFFSET);
822
/* Invalid (MULTI)Linestring */
823
if ( obj->nVertices < 2 )
826
"MULTILINESTRING %d as %d vertices, set to NULL\n",
828
if (dump_format) printf("\\N\n");
829
else printf("NULL);\n");
834
if (!dump_format) printf("'");
835
if ( sr_id > 0 ) printf("SRID=%d;", sr_id);
837
if (simple_geometries==0) // We write MULTI geometries, so generate Header
839
print_wkb_byte(getEndianByte());
840
print_wkb_int(wkbtype);
841
print_wkb_int(obj->nParts); /* npolys */
843
else if ((obj->nParts)!=1) // We write Non-MULTI geometries, but have several parts:
845
fprintf(stderr, "We have a MultiLineString with %d parts, can't use -S switch!\n", obj->nParts);
849
for (pi=0; pi<obj->nParts; pi++)
851
int vi; /* vertex index */
852
int vs; /* start vertex */
853
int ve; /* end vertex */
855
print_wkb_byte(getEndianByte());
856
print_wkb_int(subtype);
858
/* Set start and end vertexes */
859
if ( pi==obj->nParts-1 ) ve = obj->nVertices;
860
else ve = obj->panPartStart[pi+1];
861
vs = obj->panPartStart[pi];
863
print_wkb_int(ve-vs);
864
for ( vi=vs; vi<ve; vi++)
866
print_wkb_double(obj->padfX[vi]);
867
print_wkb_double(obj->padfY[vi]);
868
if ( wkbtype & WKBZOFFSET )
869
print_wkb_double(obj->padfZ[vi]);
870
if ( wkbtype & WKBMOFFSET )
871
print_wkb_double(obj->padfM[vi]);
875
if (dump_format) printf("\n");
876
else printf("');\n");
880
InsertLineStringWKT(int id)
882
int pi; /* part index */
884
/* Invalid (MULTI)Linestring */
885
if ( obj->nVertices < 2 )
888
"MULTILINESTRING %d as %d vertices, set to NULL\n",
890
if (dump_format) printf("\\N\n");
891
else printf("NULL);\n");
896
if (dump_format) printf("SRID=%d;",sr_id );
897
else printf("GeometryFromText('");
899
if (simple_geometries==0) // We write MULTI geometries, so generate Header
901
printf("MULTILINESTRING(");
903
else if ((obj->nParts)==1)
905
printf("LINESTRING");
907
else // We write Non-MULTI geometries, but have several parts:
822
LWCOLLECTION *lwcollection;
824
LWGEOM **lwmultilinestrings;
825
uchar *serialized_lwgeom;
826
LWGEOM_UNPARSER_RESULT lwg_unparser_result;
831
int dims = 0, hasz = 0, hasm = 0;
833
int u, v, start_vertex, end_vertex;
835
/* Determine the correct dimensions: note that in hwgeom-compatible mode we cannot use
837
if (wkbtype & WKBZOFFSET) hasz = 1;
839
if (wkbtype & WKBMOFFSET) hasm = 1;
840
TYPE_SETZM(dims, hasz, hasm);
842
if (simple_geometries == 1 && obj->nParts > 1)
909
844
fprintf(stderr, "We have a Multilinestring with %d parts, can't use -S switch!\n", obj->nParts);
913
for (pi=0; pi<obj->nParts; pi++)
848
/* Allocate memory for our array of LWLINEs and our dynptarrays */
849
lwmultilinestrings = malloc(sizeof(LWPOINT *) * obj->nParts);
850
dpas = malloc(sizeof(DYNPTARRAY *) * obj->nParts);
852
/* We need an array of pointers to each of our sub-geometries */
853
for (u = 0; u < obj->nParts; u++)
915
int vi; /* vertex index */
916
int vs; /* start vertex */
917
int ve; /* end vertex */
922
/* Set start and end vertexes */
923
if ( pi==obj->nParts-1 ) ve = obj->nVertices;
924
else ve = obj->panPartStart[pi+1];
925
vs = obj->panPartStart[pi];
927
for ( vi=vs; vi<ve; vi++)
855
/* Create a dynptarray containing the line points */
856
dpas[u] = dynptarray_create(obj->nParts, dims);
858
/* Set the start/end vertices depending upon whether this is
859
a MULTILINESTRING or not */
860
if ( u == obj->nParts-1 )
861
end_vertex = obj->nVertices;
863
end_vertex = obj->panPartStart[u + 1];
865
start_vertex = obj->panPartStart[u];
867
for (v = start_vertex; v < end_vertex; v++)
929
if ( vi > vs ) printf(",");
930
printf("%.15g %.15g",
933
if ( wkbtype & WKBZOFFSET )
934
printf(" %.15g", obj->padfZ[vi]);
869
/* Generate the point */
870
point4d.x = obj->padfX[v];
871
point4d.y = obj->padfY[v];
873
if (wkbtype & WKBZOFFSET)
874
point4d.z = obj->padfZ[v];
875
if (wkbtype & WKBMOFFSET)
876
point4d.m = obj->padfM[v];
878
dynptarray_addPoint4d(dpas[u], &point4d, 0);
941
if (simple_geometries==0) printf(")");
943
if (dump_format) printf("\n");
944
else printf("',%d) );\n",sr_id);
881
/* Generate the LWLINE */
882
lwmultilinestrings[u] = lwline_as_lwgeom(lwline_construct(sr_id, NULL, dpas[u]->pa));
885
/* If using MULTILINESTRINGs then generate the serialized collection, otherwise just a single LINESTRING */
886
if (simple_geometries == 0)
888
lwcollection = lwcollection_construct(MULTILINETYPE, sr_id, NULL, obj->nParts, lwmultilinestrings);
889
serialized_lwgeom = lwgeom_serialize(lwcollection_as_lwgeom(lwcollection));
893
serialized_lwgeom = lwgeom_serialize(lwmultilinestrings[0]);
897
result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE, -1);
899
result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE);
903
fprintf(stderr, "ERROR: %s\n", lwg_unparser_result.message);
907
OutputGeometry(lwg_unparser_result.wkoutput);
909
/* Free all of the allocated items */
910
lwfree(lwg_unparser_result.wkoutput);
911
lwfree(serialized_lwgeom);
913
for (u = 0; u < obj->nParts; u++)
915
lwline_free(lwgeom_as_lwline(lwmultilinestrings[u]));
920
lwfree(lwmultilinestrings);
1103
/*This function basically deals with the polygon case. */
1104
/*it sorts the polys in order of outer,inner,inner, so that inners */
1105
/*always come after outers they are within */
1073
/** @brief This function basically deals with the polygon case. */
1074
/* it sorts the polys in order of outer,inner,inner, so that inners */
1075
/* always come after outers they are within */
1107
1077
InsertPolygon(void)
1109
unsigned int subtype = POLYGONTYPE | (wkbtype&WKBZOFFSET) |
1110
(wkbtype&WKBMOFFSET);
1113
int pi; /* part index */
1115
out_index = FindPolygons(obj, &Outer);
1117
if (!dump_format) printf("'");
1118
if ( sr_id > 0 ) printf("SRID=%d;", sr_id);
1120
if (simple_geometries==0) // We write MULTI geometries, so generate Header
1122
print_wkb_byte(getEndianByte());
1123
print_wkb_int(wkbtype);
1124
print_wkb_int(out_index); /* npolys */
1126
else if (out_index!=1) // We write Non-MULTI geometries, but have several parts:
1128
fprintf(stderr, "We have a Multipolygon with %d parts, can't use -S switch!\n", out_index);
1132
/* Write the coordinates */
1133
for(pi=0; pi<out_index; pi++)
1139
print_wkb_byte(getEndianByte());
1140
print_wkb_int(subtype);
1141
print_wkb_int(poly->linked+1); /* nrings */
1145
int vi; /* vertex index */
1147
print_wkb_int(poly->n); /* npoints */
1149
for(vi=0; vi<poly->n; vi++)
1151
print_wkb_double(poly->list[vi].x);
1152
print_wkb_double(poly->list[vi].y);
1153
if ( wkbtype & WKBZOFFSET )
1154
print_wkb_double(poly->list[vi].z);
1155
if ( wkbtype & WKBMOFFSET )
1156
print_wkb_double(poly->list[vi].m);
1164
if (dump_format) printf("\n");
1165
else printf("');\n");
1167
/* Release all memory */
1168
ReleasePolygons(Outer, out_index);
1173
InsertPolygonWKT(void)
1175
Ring **Outer; /* Pointers to Outer rings */
1176
int out_index=0; /* Count of Outer rings */
1177
int pi; /* part index */
1180
static int call = -1;
1183
fprintf(stderr, "InsertPolygon[%d]: allocated space for %d rings\n",
1187
out_index = FindPolygons(obj, &Outer);
1189
if (dump_format) printf("SRID=%d;",sr_id );
1190
else printf("GeometryFromText('");
1192
if (simple_geometries==0) // We write MULTI geometries, so generate Header
1194
printf("MULTIPOLYGON(");
1196
else if (out_index==1)
1201
{ // We write Non-MULTI geometries, but have several parts:
1202
fprintf(stderr, "We have a Multipolygon with %d parts, can't use -S switch!\n", out_index);
1206
/* Write the coordinates */
1207
for(pi=0; pi<out_index; pi++)
1213
if ( pi ) printf(",");
1218
int vi; /* vertex index */
1221
for(vi=0; vi<poly->n; vi++)
1223
if ( vi ) printf(",");
1224
printf("%.15g %.15g",
1227
if ( wkbtype & WKBZOFFSET )
1228
printf(" %.15g", poly->list[vi].z);
1233
if ( poly ) printf(",");
1240
if (simple_geometries==0) printf(")");
1242
if (dump_format) printf("\n");
1243
else printf("',%d) );\n",sr_id);
1245
/* Release all memory */
1246
ReleasePolygons(Outer, out_index);
1080
int polygon_total, ring_total;
1081
int pi, vi; // part index and vertex index
1084
LWCOLLECTION *lwcollection = NULL;
1086
LWGEOM **lwpolygons;
1087
uchar *serialized_lwgeom;
1088
LWGEOM_UNPARSER_RESULT lwg_unparser_result;
1095
int dims = 0, hasz = 0, hasm = 0;
1098
/* Determine the correct dimensions: note that in hwgeom-compatible mode we cannot use
1100
if (wkbtype & WKBZOFFSET) hasz = 1;
1102
if (wkbtype & WKBMOFFSET) hasm = 1;
1103
TYPE_SETZM(dims, hasz, hasm);
1105
polygon_total = FindPolygons(obj, &Outer);
1107
if (simple_geometries == 1 && polygon_total != 1) /* We write Non-MULTI geometries, but have several parts: */
1109
fprintf(stderr, "We have a Multipolygon with %d parts, can't use -S switch!\n", polygon_total);
1113
/* Allocate memory for our array of LWPOLYs */
1114
lwpolygons = malloc(sizeof(LWPOLY *) * polygon_total);
1116
/* Allocate memory for our POINTARRAY pointers for each polygon */
1117
pas = malloc(sizeof(POINTARRAY **) * polygon_total);
1119
/* Cycle through each individual polygon */
1120
for(pi = 0; pi < polygon_total; pi++)
1125
/* Firstly count through the total number of rings in this polygon */
1127
polyring = Outer[pi];
1131
polyring = polyring->next;
1134
/* Reserve memory for the POINTARRAYs representing each ring */
1135
pas[pi] = malloc(sizeof(POINTARRAY *) * ring_total);
1137
/* Cycle through each ring within the polygon, starting with the outer */
1138
polyring = Outer[pi];
1142
/* Create a DYNPTARRAY containing the points making up the ring */
1143
dpas = dynptarray_create(polyring->n, dims);
1145
for(vi = 0; vi < polyring->n; vi++)
1147
/* Build up a point array of all the points in this ring */
1148
point4d.x = polyring->list[vi].x;
1149
point4d.y = polyring->list[vi].y;
1151
if (wkbtype & WKBZOFFSET)
1152
point4d.z = polyring->list[vi].z;
1153
if (wkbtype & WKBMOFFSET)
1154
point4d.m = polyring->list[vi].m;
1156
dynptarray_addPoint4d(dpas, &point4d, 0);
1159
/* Copy the POINTARRAY pointer from the DYNPTARRAY structure so we can
1160
use the LWPOLY constructor */
1161
pas[pi][ring_index] = dpas->pa;
1163
/* Free the DYNPTARRAY structure (we don't need this part anymore as we
1164
have the reference to the internal POINTARRAY) */
1167
polyring = polyring->next;
1171
/* Generate the LWGEOM */
1172
lwpoly = lwpoly_construct(sr_id, NULL, ring_total, pas[pi]);
1173
lwpolygons[pi] = lwpoly_as_lwgeom(lwpoly);
1176
ReleasePolygons(Outer, polygon_total);
1178
/* If using MULTIPOLYGONS then generate the serialized collection, otherwise just a single POLYGON */
1179
if (simple_geometries == 0)
1181
lwcollection = lwcollection_construct(MULTIPOLYGONTYPE, sr_id, NULL, polygon_total, lwpolygons);
1182
serialized_lwgeom = lwgeom_serialize(lwcollection_as_lwgeom(lwcollection));
1186
serialized_lwgeom = lwgeom_serialize(lwpolygons[0]);
1190
result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE, -1);
1192
result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE);
1196
fprintf(stderr, "ERROR: %s\n", lwg_unparser_result.message);
1200
OutputGeometry(lwg_unparser_result.wkoutput);
1202
/* Free all of the allocated items */
1203
lwfree(lwg_unparser_result.wkoutput);
1204
lwfree(serialized_lwgeom);
1206
/* Cycle through each polygon, freeing everything we need... */
1207
for (u = 0; u < polygon_total; u++)
1208
lwpoly_free(lwgeom_as_lwpoly(lwpolygons[u]));
1210
/* Free the pointer arrays */
1213
if (simple_geometries == 0)
1214
lwfree(lwcollection);
1218
* @brief Insert either a POINT or MULTIPOINT into the output stream
1251
1221
InsertPoint(void)
1253
if (!dump_format) printf("'");
1254
if ( sr_id > 0 ) printf("SRID=%d;", sr_id);
1256
print_wkb_byte(getEndianByte());
1257
print_wkb_int(wkbtype);
1258
print_wkb_double(obj->padfX[0]);
1259
print_wkb_double(obj->padfY[0]);
1260
if ( wkbtype & WKBZOFFSET ) print_wkb_double(obj->padfZ[0]);
1261
if ( wkbtype & WKBMOFFSET ) print_wkb_double(obj->padfM[0]);
1263
if (dump_format) printf("\n");
1264
else printf("');\n");
1268
InsertPointWKT(void)
1271
if (dump_format) printf("SRID=%d;%s(", sr_id, pgtype);
1272
else printf("GeometryFromText('%s(", pgtype);
1274
for (u=0;u<obj->nVertices; u++){
1275
if (u>0) printf(",");
1276
printf("%.15g %.15g",obj->padfX[u],obj->padfY[u]);
1277
if ( wkbtype & WKBZOFFSET ) printf(" %.15g", obj->padfZ[u]);
1279
if (dump_format) printf(")\n");
1280
else printf(")',%d) );\n",sr_id);
1285
InsertMultiPoint(void)
1288
unsigned int subtype = POINTTYPE | (wkbtype&WKBZOFFSET) |
1289
(wkbtype&WKBMOFFSET);
1291
if (!dump_format) printf("'");
1292
if ( sr_id > 0 ) printf("SRID=%d;", sr_id);
1294
print_wkb_byte(getEndianByte());
1295
print_wkb_int(wkbtype);
1296
print_wkb_int(obj->nVertices);
1298
for (u=0;u<obj->nVertices; u++)
1300
print_wkb_byte(getEndianByte());
1301
print_wkb_int(subtype);
1302
print_wkb_double(obj->padfX[u]);
1303
print_wkb_double(obj->padfY[u]);
1304
if ( wkbtype & WKBZOFFSET ) print_wkb_double(obj->padfZ[u]);
1305
if ( wkbtype & WKBMOFFSET ) print_wkb_double(obj->padfM[u]);
1308
if (dump_format) printf("\n");
1309
else printf("');\n");
1223
LWCOLLECTION *lwcollection;
1225
LWGEOM **lwmultipoints;
1226
uchar *serialized_lwgeom;
1227
LWGEOM_UNPARSER_RESULT lwg_unparser_result;
1232
int dims = 0, hasz = 0, hasm = 0;
1236
/* Determine the correct dimensions: note that in hwgeom-compatible mode we cannot use
1238
if (wkbtype & WKBZOFFSET) hasz = 1;
1240
if (wkbtype & WKBMOFFSET) hasm = 1;
1241
TYPE_SETZM(dims, hasz, hasm);
1243
/* Allocate memory for our array of LWPOINTs and our dynptarrays */
1244
lwmultipoints = malloc(sizeof(LWPOINT *) * obj->nVertices);
1245
dpas = malloc(sizeof(DYNPTARRAY *) * obj->nVertices);
1247
/* We need an array of pointers to each of our sub-geometries */
1248
for (u = 0; u < obj->nVertices; u++)
1250
/* Generate the point */
1251
point4d.x = obj->padfX[u];
1252
point4d.y = obj->padfY[u];
1254
if (wkbtype & WKBZOFFSET)
1255
point4d.z = obj->padfZ[u];
1256
if (wkbtype & WKBMOFFSET)
1257
point4d.m = obj->padfM[u];
1259
/* Create a dynptarray containing a single point */
1260
dpas[u] = dynptarray_create(1, dims);
1261
dynptarray_addPoint4d(dpas[u], &point4d, 0);
1263
/* Generate the LWPOINT */
1264
lwmultipoints[u] = lwpoint_as_lwgeom(lwpoint_construct(sr_id, NULL, dpas[u]->pa));
1267
/* If we have more than 1 vertex then we are working on a MULTIPOINT and so generate a MULTIPOINT
1268
rather than a POINT */
1269
if (obj->nVertices > 1)
1271
lwcollection = lwcollection_construct(MULTIPOINTTYPE, sr_id, NULL, obj->nVertices, lwmultipoints);
1272
serialized_lwgeom = lwgeom_serialize(lwcollection_as_lwgeom(lwcollection));
1276
serialized_lwgeom = lwgeom_serialize(lwmultipoints[0]);
1280
result = serialized_lwgeom_to_hexwkb(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE, -1);
1282
result = serialized_lwgeom_to_ewkt(&lwg_unparser_result, serialized_lwgeom, PARSER_CHECK_NONE);
1286
fprintf(stderr, "ERROR: %s\n", lwg_unparser_result.message);
1290
OutputGeometry(lwg_unparser_result.wkoutput);
1292
/* Free all of the allocated items */
1293
lwfree(lwg_unparser_result.wkoutput);
1294
lwfree(serialized_lwgeom);
1296
for (u = 0; u < obj->nVertices; u++)
1298
lwpoint_free(lwgeom_as_lwpoint(lwmultipoints[u]));
1303
lwfree(lwmultipoints);
1307
OutputGeometry(char *geometry)
1309
/* This function outputs the specified geometry string (WKB or WKT) formatted
1310
* according to whether we have specified dump format or hwgeom format */
1315
printf("GeomFromText('");
1318
/* Output SRID if relevant */
1320
printf("SRID=%d;", sr_id);
1323
printf("%s", geometry);
1329
/* Output SRID if relevant */
1331
printf(", %d)", sr_id);
1343
printf("%s", geometry);
1313
1354
ParseCmdline(int ARGC, char **ARGV)