-
Notifications
You must be signed in to change notification settings - Fork 5
/
to_kml.c
130 lines (107 loc) · 3.69 KB
/
to_kml.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
#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_kml [FILENAME]\n"); exit(1); }
DBFHandle d = DBFOpen(argv[1], "rb");
if (d == NULL) { printf("DBFOpen error (%s.dbf)\n", argv[1]); exit(1); }
SHPHandle h = SHPOpen(argv[1], "rb");
if (h == NULL) { printf("SHPOpen error (%s.dbf)\n", argv[1]); exit(1); }
char filename[60];
sprintf(filename, "%s.kml", 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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(fp, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
/*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, " <Document>\n");
int i;
for (i = 0 ; i < nRecordCount ; i++)
{
fprintf(fp, " <Placemark>\n");
fprintf(fp, " <name>%s</name>\n", (char *)DBFReadStringAttribute(d, i, 2));
fprintf(fp, " <Polygon>\n");
fprintf(fp, " <extrude>1</extrude>\n");
fprintf(fp, " <altitudeMode>relativeToGround</altitudeMode>\n");
fprintf(fp, " <outerBoundaryIs>\n");
fprintf(fp, " <LinearRing>\n");
fprintf(fp, " <coordinates>\n");
SHPObject *psShape = SHPReadObject(h, i);
int j, iPart;
for (j = 0, iPart = 1; j < psShape->nVertices; j++)
{
fprintf(fp, "%f,%f,100\n", psShape->padfX[j], psShape->padfY[j]);
}
fprintf(fp, " </coordinates>\n");
fprintf(fp, " </LinearRing>\n");
fprintf(fp, " </outerBoundaryIs>\n");
fprintf(fp, " </Polygon>\n");
fprintf(fp, " </Placemark>\n");
}
fprintf(fp, " </Document>\n");
/*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);
for (i = 0; i < nEntities; i++)
{
SHPObject *psShape = SHPReadObject(h, i);
//fprintf(fp, "INSERT INTO edges (id) VALUES (%d);\n", i+1);
int j, iPart;
for (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 vertexes (edge_id, x, y) VALUES (", (j!=0 ? ");\n": ""));
//else
// fprintf(fp, "),(");
//fprintf(fp, "%d, %f, %f", i+1, psShape->padfX[j], psShape->padfY[j]);
}
//fprintf(fp, ");\n");
SHPDestroyObject(psShape);
}*/
fprintf(fp, "</kml>\n");
printf("all done\n");
fclose(fp);
if (h != NULL) SHPClose(h);
if (d != NULL) DBFClose(d);
}