-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathto_mysql.c
176 lines (154 loc) · 4.99 KB
/
to_mysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shapefile_src/shapefil.h"
int main(int argc, char **argv)
{
if (argc == 1) { printf("usage: shapefile_to_mysqldump [FILENAME]\n"); exit(1); }
setlocale(LC_CTYPE, "en_CA.UTF-8");
char file_name[150];
sprintf(file_name, "%s", argv[1]);
DBFHandle d = DBFOpen(file_name, "rb");
if (d == NULL)
{
sprintf(file_name, "%s/%s", argv[1], argv[1]);
d = DBFOpen(file_name, "rb");
if (d == NULL)
{
printf("DBFOpen error (%s.dbf)\n", argv[1]);
exit(1);
}
}
char filename[60];
sprintf(filename, "%s.sql", argv[1]);
printf("%s\n", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL) { printf("fopen error\n"); exit(1); }
int nRecordCount = DBFGetRecordCount(d);
int nFieldCount = DBFGetFieldCount(d);
printf("DBF has %d records (with %d fields)\n", nRecordCount, nFieldCount);
fprintf(fp, "SET CHARSET UTF8;\n");
fprintf(fp, "DROP TABLE IF EXISTS DBF;\n");
fprintf(fp, "CREATE TABLE DBF (dbf_id INT primary key auto_increment");
for (int i = 0 ; i < nFieldCount ; i++)
{
char pszFieldName[12];
int pnWidth;
int pnDecimals;
DBFFieldType ft = DBFGetFieldInfo(d, i, pszFieldName, &pnWidth, &pnDecimals);
switch (ft)
{
case FTString:
fprintf(fp, ", %s VARCHAR(%d)", pszFieldName, pnWidth);
break;
case FTInteger:
fprintf(fp, ", %s INT", pszFieldName);
break;
case FTDouble:
fprintf(fp, ", %s FLOAT(15,10)", pszFieldName);
break;
case FTLogical:
break;
case FTInvalid:
break;
}
}
fprintf(fp, ");\n");
for (int i = 0 ; i < nRecordCount ; i++)
{
unsigned int k;
char *cStr;
char pszFieldName[12];
int pnWidth;
int pnDecimals;
fprintf(fp, "INSERT INTO DBF VALUES (''");
for (int j = 0 ; j < nFieldCount ; j++)
{
DBFFieldType ft = DBFGetFieldInfo(d, j, pszFieldName, &pnWidth, &pnDecimals);
switch (ft)
{
case FTString:
cStr = (char *)DBFReadStringAttribute(d, i, j);
for (k = 0 ; k < strlen(cStr) ; k++)
if (cStr[k] == '"')
cStr[k] = '\'';
fprintf(fp, ",\"%s\"", cStr);
break;
case FTInteger:
fprintf(fp, ",\"%d\"", DBFReadIntegerAttribute(d, i, j));
break;
case FTDouble:
fprintf(fp, ",\"%f\"", DBFReadDoubleAttribute(d, i, j));
break;
case FTLogical:
break;
case FTInvalid:
break;
}
}
fprintf(fp, ");\n");
}
SHPHandle h = SHPOpen(file_name, "rb");
if (h == NULL)
{
printf("SHPOpen error (%s.dbf)\n", argv[1]);
exit(1);
}
int nShapeType;
int nEntities;
const char *pszPlus;
double adfMinBound[4], adfMaxBound[4];
SHPGetInfo(h, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
printf("SHP has %d entities\n", nEntities);
fprintf(fp, "DROP TABLE IF EXISTS shape_points;\n");
fprintf(fp, "CREATE TABLE shape_points (id INT PRIMARY KEY AUTO_INCREMENT, dbf_id MEDIUMINT, part_id MEDIUMINT, x DOUBLE(18,15), y DOUBLE(18,15));\n");
fprintf(fp, "DROP TABLE IF EXISTS edges;\n");
fprintf(fp, "CREATE TABLE edges (id INT PRIMARY KEY AUTO_INCREMENT, dbf_id MEDIUMINT, part_id MEDIUMINT, part_type VARCHAR(50));\n");
fprintf(fp, "ALTER TABLE shape_points ADD KEY dbf_id (dbf_id);\n");
for (int i = 0; i < nEntities; i++)
{
SHPObject *psShape = SHPReadObject(h, i);
for (int j = 0, iPart = 1; j < psShape->nVertices; j++)
{
const char *pszPartType = "";
if (j == 0 && psShape->nParts > 0)
{
pszPartType = SHPPartTypeName(psShape->panPartType[0]);
fprintf(fp, "INSERT INTO edges (dbf_id, part_id, part_type) VALUES (%d, %d, \"%s\");\n", i+1, iPart, pszPartType);
}
if (iPart < psShape->nParts && psShape->panPartStart[iPart] == j)
{
pszPartType = SHPPartTypeName(psShape->panPartType[iPart]);
iPart++;
pszPlus = "+";
fprintf(fp, "INSERT INTO edges (dbf_id, part_id, part_type) VALUES (%d, %d, \"%s\");\n", i+1, iPart, pszPartType);
}
else
pszPlus = " ";
}
for (int j = 0, iPart = 1; j < psShape->nVertices; j++)
{
const char *pszPartType = "";
if (j == 0 && psShape->nParts > 0) pszPartType = SHPPartTypeName(psShape->panPartType[0]);
if (iPart < psShape->nParts && psShape->panPartStart[iPart] == j)
{
pszPartType = SHPPartTypeName(psShape->panPartType[iPart]);
iPart++;
pszPlus = "+";
}
else
pszPlus = " ";
if (j%500==0)
fprintf(fp, "%sINSERT INTO shape_points (dbf_id, part_id, x, y) VALUES (", (j!=0 ? ");\n": ""));
else
fprintf(fp, "),(");
fprintf(fp, "%d, %d, %3.15lf, %3.15lf", i+1, iPart, psShape->padfX[j], psShape->padfY[j]);
}
fprintf(fp, ");\n");
SHPDestroyObject(psShape);
}
printf("all done\n");
if (h != NULL) SHPClose(h);
if (d != NULL) DBFClose(d);
}