summaryrefslogtreecommitdiff
path: root/navit/map/shapefile/shapefile.c
blob: f2180ef41803bdfb4c71b1d74d13a0e07ca32172 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "config.h"
#include "debug.h"
#include "plugin.h"
#include "projection.h"
#include "item.h"
#include "map.h"
#include "maptype.h"
#include "attr.h"
#include "transform.h"
#include "file.h"
#include "shapefil.h"


struct map_priv {
	int id;
	char *filename;
	char *charset;
	SHPHandle hSHP;
	DBFHandle hDBF;
	int nShapeType, nEntities, nFields;
	double adfMinBound[4], adfMaxBound[4];
};


struct map_rect_priv {
	struct map_selection *sel;
	struct map_priv *m;
	struct item item;
	int idx;
	int cidx;
	int aidx;
	enum attr_type anext;
	SHPObject *psShape;
	char *str;
};

static void
map_destroy_shapefile(struct map_priv *m)
{
	dbg(1,"map_destroy_shapefile\n");
	g_free(m);
}

static void
shapefile_coord_rewind(void *priv_data)
{
	struct map_rect_priv *mr=priv_data;
	mr->cidx=0;
}

static int
shapefile_coord_get(void *priv_data, struct coord *c, int count)
{
	struct map_rect_priv *mr=priv_data;
	int ret=0;
	int idx;
	struct coord_geo g;
	SHPObject *psShape=mr->psShape;
	while (count) {
		idx=mr->cidx;
		if (idx >= psShape->nVertices)
			break;
		g.lng=psShape->padfX[idx];
		g.lat=psShape->padfY[idx];
		transform_from_geo(projection_mg, &g, c);
		mr->cidx++;
		ret++;
		c++;
		count--;
	}
	return ret;
}

static void
shapefile_attr_rewind(void *priv_data)
{
	struct map_rect_priv *mr=priv_data;
	mr->aidx=0;
	mr->anext=attr_debug;
}

static int
shapefile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
{	
	struct map_rect_priv *mr=priv_data;
	struct map_priv *m=mr->m;
	char szTitle[12],*pszTypeName,*str;
	int ret, nWidth, nDecimals;

	attr->type=attr_type;
	switch (attr_type) {
	case attr_any:
		while (mr->anext != attr_none) {
			ret=shapefile_attr_get(priv_data, mr->anext, attr);
			if (ret)
				return ret;
		}
		return 0;
	case attr_debug:
		if (mr->aidx >= m->nFields) {
			mr->anext=attr_none;
			return 0;
		}
		switch (DBFGetFieldInfo(m->hDBF, mr->aidx, szTitle, &nWidth, &nDecimals )) {
		case FTString:
			pszTypeName = "String";
			str=g_strdup(DBFReadStringAttribute( m->hDBF, mr->item.id_lo, mr->aidx ));
			break;
		case FTInteger:
			pszTypeName = "Integer";
			str=g_strdup_printf("%d",DBFReadIntegerAttribute( m->hDBF, mr->item.id_lo, mr->aidx ));
			break;
		case FTDouble:
			pszTypeName = "Double";
			str=g_strdup_printf("%lf",DBFReadDoubleAttribute( m->hDBF, mr->item.id_lo, mr->aidx ));
			break;
		case FTInvalid:
			pszTypeName = "Invalid";
			str=NULL;
			break;
		default:
			pszTypeName = "Unknown";
			str=NULL;
		}
		g_free(mr->str);
		mr->str=attr->u.str=g_strdup_printf("%s=%s(%s)",szTitle,str,pszTypeName);
		g_free(str);
		mr->aidx++;
		return 1;
	default:
		return 0;
	}
}

static struct item_methods methods_shapefile = {
        shapefile_coord_rewind,
        shapefile_coord_get,
        shapefile_attr_rewind,
        shapefile_attr_get,
};

static struct map_rect_priv *
map_rect_new_shapefile(struct map_priv *map, struct map_selection *sel)
{
	struct map_rect_priv *mr;

	dbg(1,"map_rect_new_shapefile\n");
	mr=g_new0(struct map_rect_priv, 1);
	mr->m=map;
	mr->idx=0;
	mr->item.id_lo=0;
	mr->item.id_hi=0;
	mr->item.meth=&methods_shapefile;
	mr->item.priv_data=mr;
	return mr;
}


static void
map_rect_destroy_shapefile(struct map_rect_priv *mr)
{
	if (mr->psShape)
		SHPDestroyObject(mr->psShape);
	g_free(mr->str);
        g_free(mr);
}

static struct item *
map_rect_get_item_shapefile(struct map_rect_priv *mr)
{
	struct map_priv *m=mr->m;
	if (mr->idx >= m->nEntities)
		return NULL;
	mr->item.type=type_street_unkn;
	mr->item.id_lo=mr->idx;
	if (mr->psShape)
		SHPDestroyObject(mr->psShape);
	mr->psShape=SHPReadObject(m->hSHP, mr->idx);
	mr->idx++;
	shapefile_coord_rewind(mr);
	shapefile_attr_rewind(mr);
	return &mr->item;
}

static struct item *
map_rect_get_item_byid_shapefile(struct map_rect_priv *mr, int id_hi, int id_lo)
{
	mr->idx=id_lo;
	return map_rect_get_item_shapefile(mr);
}

static struct map_methods map_methods_shapefile = {
	projection_mg,
	"iso8859-1",
	map_destroy_shapefile,
	map_rect_new_shapefile,
	map_rect_destroy_shapefile,
	map_rect_get_item_shapefile,
	map_rect_get_item_byid_shapefile,
};

static struct map_priv *
map_new_shapefile(struct map_methods *meth, struct attr **attrs)
{
	struct map_priv *m;
	struct attr *data=attr_search(attrs, NULL, attr_data);
	struct attr *charset=attr_search(attrs, NULL, attr_charset);
	struct file_wordexp *wexp;
	char *wdata;
	char **wexp_data;
	char *shapefile,*dbffile;
	if (! data)
		return NULL;
	dbg(1,"map_new_shapefile %s\n", data->u.str);	
	wdata=g_strdup(data->u.str);
	wexp=file_wordexp_new(wdata);
	wexp_data=file_wordexp_get_array(wexp);
	*meth=map_methods_shapefile;

	m=g_new0(struct map_priv, 1);
	m->filename=g_strdup(wexp_data[0]);
	shapefile=g_strdup_printf("%s.shp", m->filename);
	m->hSHP=SHPOpen(shapefile, "rb" );
	SHPGetInfo( m->hSHP, &m->nEntities, &m->nShapeType, m->adfMinBound, m->adfMaxBound );
	g_free(shapefile);
	dbffile=g_strdup_printf("%s.dbf", m->filename);
	m->hDBF=DBFOpen(dbffile, "rb");
	m->nFields=DBFGetFieldCount(m->hDBF);
	g_free(dbffile);
	dbg(1,"map_new_shapefile %s %s\n", m->filename, wdata);
	if (charset) {
		m->charset=g_strdup(charset->u.str);
		meth->charset=m->charset;
	}
	file_wordexp_destroy(wexp);
	return m;
}

void
plugin_init(void)
{
	dbg(1,"shapefile: plugin_init\n");
	plugin_register_map_type("shapefile", map_new_shapefile);
}

/************************************************************************/
/*                            VSI_SHP_Open()                            */
/************************************************************************/

static SAFile VSI_SHP_Open( const char *pszFilename, const char *pszAccess )

{
	return (SAFile)fopen(pszFilename, pszAccess);
}

/************************************************************************/
/*                            VSI_SHP_Read()                            */
/************************************************************************/

static SAOffset VSI_SHP_Read( void *p, SAOffset size, SAOffset nmemb, SAFile file )

{
	return fread(p, size, nmemb, (FILE *)file);
}

/************************************************************************/
/*                           VSI_SHP_Write()                            */
/************************************************************************/

static SAOffset VSI_SHP_Write( void *p, SAOffset size, SAOffset nmemb, SAFile file )

{
	return fwrite(p, size, nmemb, (FILE *)file);
}

/************************************************************************/
/*                            VSI_SHP_Seek()                            */
/************************************************************************/

static SAOffset VSI_SHP_Seek( SAFile file, SAOffset offset, int whence )

{
	return fseek((FILE *)file, offset, whence);
}

/************************************************************************/
/*                            VSI_SHP_Tell()                            */
/************************************************************************/

static SAOffset VSI_SHP_Tell( SAFile file )

{
	return ftell((FILE *)file);
}


static int VSI_SHP_Flush( SAFile file )

{
	return fflush((FILE *)file);
}

/************************************************************************/
/*                           VSI_SHP_Close()                            */
/************************************************************************/

static int VSI_SHP_Close( SAFile file )

{
	return fclose((FILE *)file);
}

/************************************************************************/
/*                              SADError()                              */
/************************************************************************/

static void VSI_SHP_Error( const char *message )

{
	dbg(0,"error:%s\n", message);
}

/************************************************************************/
/*                           VSI_SHP_Remove()                           */
/************************************************************************/

static int VSI_SHP_Remove( const char *pszFilename )

{
	return unlink(pszFilename);
}

/************************************************************************/
/*                        SASetupDefaultHooks()                         */
/************************************************************************/

void SASetupDefaultHooks( SAHooks *psHooks )

{
    psHooks->FOpen   = VSI_SHP_Open;
    psHooks->FRead   = VSI_SHP_Read;
    psHooks->FWrite  = VSI_SHP_Write;
    psHooks->FSeek   = VSI_SHP_Seek;
    psHooks->FTell   = VSI_SHP_Tell;
    psHooks->FFlush  = VSI_SHP_Flush;
    psHooks->FClose  = VSI_SHP_Close;

    psHooks->Remove  = VSI_SHP_Remove;
    psHooks->Atof    = atof;

    psHooks->Error   = VSI_SHP_Error;
}