1 : /******************************************************************************
2 : * $Id: ntf.h 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: NTF Translator
5 : * Purpose: Main declarations for NTF translator.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef _NTF_H_INCLUDED
31 : #define _NTF_H_INCLUDED
32 :
33 : #include "cpl_conv.h"
34 : #include "ogrsf_frmts.h"
35 :
36 : /* -------------------------------------------------------------------- */
37 : /* Record types. */
38 : /* -------------------------------------------------------------------- */
39 : #define NRT_VHR 1 /* Volume Header Record */
40 : #define NRT_DHR 2 /* Database Header Record */
41 : #define NRT_FCR 5 /* Feature Classification Record */
42 : #define NRT_SHR 7 /* Section Header Record */
43 : #define NRT_NAMEREC 11 /* Name Record */
44 : #define NRT_NAMEPOSTN 12 /* Name Position */
45 : #define NRT_ATTREC 14 /* Attribute Record */
46 : #define NRT_POINTREC 15 /* Point Record */
47 : #define NRT_NODEREC 16 /* Node Record */
48 : #define NRT_GEOMETRY 21 /* Geometry Record */
49 : #define NRT_GEOMETRY3D 22 /* 3D Geometry Record */
50 : #define NRT_LINEREC 23 /* Line Record */
51 : #define NRT_CHAIN 24 /* Chain */
52 : #define NRT_POLYGON 31 /* Polygon */
53 : #define NRT_CPOLY 33 /* Complex Polygon */
54 : #define NRT_COLLECT 34 /* Collection of featues */
55 : #define NRT_ADR 40 /* Attribute Description Record */
56 : #define NRT_CODELIST 42 /* Codelist Record (ie. BL2000) */
57 : #define NRT_TEXTREC 43 /* Text */
58 : #define NRT_TEXTPOS 44 /* Text position */
59 : #define NRT_TEXTREP 45 /* Text representation */
60 : #define NRT_GRIDHREC 50 /* Grid Header Record */
61 : #define NRT_GRIDREC 51 /* Grid Data Record */
62 : #define NRT_COMMENT 90 /* Comment record */
63 : #define NRT_VTR 99 /* Volume Termination Record */
64 :
65 : /* -------------------------------------------------------------------- */
66 : /* Product names (DBNAME) and codes. */
67 : /* -------------------------------------------------------------------- */
68 :
69 : #define NPC_UNKNOWN 0
70 :
71 : #define NPC_LANDLINE 1
72 : #define NPC_LANDLINE99 2
73 : #define NTF_LANDLINE "LAND-LINE.93"
74 : #define NTF_LANDLINE_PLUS "LAND-LINE.93+"
75 :
76 : #define NPC_STRATEGI 3
77 : #define NTF_STRATEGI "Strategi_02.96"
78 :
79 : #define NPC_MERIDIAN 4
80 : #define NTF_MERIDIAN "Meridian_01.95"
81 :
82 : #define NPC_BOUNDARYLINE 5
83 : #define NTF_BOUNDARYLINE "Boundary-Line"
84 :
85 : #define NPC_BASEDATA 6
86 : #define NTF_BASEDATA "BaseData.GB_01.96"
87 :
88 : #define NPC_OSCAR_ASSET 7
89 : #define NPC_OSCAR_TRAFFIC 8
90 : #define NPC_OSCAR_ROUTE 9
91 : #define NPC_OSCAR_NETWORK 10
92 :
93 : #define NPC_ADDRESS_POINT 11
94 :
95 : #define NPC_CODE_POINT 12
96 : #define NPC_CODE_POINT_PLUS 13
97 :
98 : #define NPC_LANDFORM_PROFILE_CONT 14
99 :
100 : #define NPC_LANDRANGER_CONT 15
101 : #define NTF_LANDRANGER_CONT "OS_LANDRANGER_CONT"
102 :
103 : #define NPC_LANDRANGER_DTM 16
104 : #define NPC_LANDFORM_PROFILE_DTM 17
105 :
106 : #define NPC_BL2000 18
107 :
108 : #define NPC_MERIDIAN2 19
109 : #define NTF_MERIDIAN2 "Meridian_02.01"
110 :
111 : /************************************************************************/
112 : /* NTFRecord */
113 : /************************************************************************/
114 :
115 : class NTFRecord
116 : {
117 : int nType;
118 : int nLength;
119 : char *pszData;
120 :
121 : int ReadPhysicalLine( FILE *fp, char *pszLine );
122 :
123 : public:
124 : NTFRecord( FILE * );
125 : ~NTFRecord();
126 :
127 3612471 : int GetType() { return nType; }
128 2 : int GetLength() { return nLength; }
129 25557 : const char *GetData() { return pszData; }
130 :
131 : const char *GetField( int, int );
132 : };
133 :
134 : /************************************************************************/
135 : /* NTFGenericClass */
136 : /************************************************************************/
137 :
138 : class NTFGenericClass
139 : {
140 : public:
141 : int nFeatureCount;
142 :
143 : int b3D;
144 : int nAttrCount;
145 : char **papszAttrNames;
146 : char **papszAttrFormats;
147 : int *panAttrMaxWidth;
148 : int *pabAttrMultiple;
149 :
150 : NTFGenericClass();
151 : ~NTFGenericClass();
152 :
153 : void CheckAddAttr( const char *, const char *, int );
154 : void SetMultiple( const char * );
155 : };
156 :
157 : /************************************************************************/
158 : /* NTFCodeList */
159 : /************************************************************************/
160 :
161 : class NTFCodeList
162 : {
163 : public:
164 : NTFCodeList( NTFRecord * );
165 : ~NTFCodeList();
166 :
167 : const char *Lookup( const char * );
168 :
169 : char szValType[3]; /* attribute code for list, ie. AC */
170 : char szFInter[6]; /* format of code values */
171 :
172 : int nNumCode;
173 : char **papszCodeVal; /* Short code value */
174 : char **papszCodeDes; /* Long description of code */
175 :
176 : };
177 :
178 : /************************************************************************/
179 : /* NTFAttDesc */
180 : /************************************************************************/
181 : typedef struct
182 : {
183 : char val_type [ 2 +1];
184 : char fwidth [ 3 +1];
185 : char finter [ 5 +1];
186 : char att_name [ 100 ];
187 :
188 : NTFCodeList *poCodeList;
189 :
190 : } NTFAttDesc;
191 :
192 :
193 : class OGRNTFLayer;
194 : class OGRNTFRasterLayer;
195 : class OGRNTFDataSource;
196 : class NTFFileReader;
197 :
198 : #define MAX_REC_GROUP 100
199 : typedef OGRFeature *(*NTFFeatureTranslator)(NTFFileReader *,
200 : OGRNTFLayer *,
201 : NTFRecord **);
202 : typedef int (*NTFRecordGrouper)(NTFFileReader *, NTFRecord **, NTFRecord *);
203 :
204 : /************************************************************************/
205 : /* NTFFileReader */
206 : /************************************************************************/
207 :
208 : class NTFFileReader
209 : {
210 : char *pszFilename;
211 : OGRNTFDataSource *poDS;
212 :
213 : FILE *fp;
214 :
215 : // feature class list.
216 : int nFCCount;
217 : char **papszFCNum;
218 : char **papszFCName;
219 :
220 : // attribute definitions
221 : int nAttCount;
222 : NTFAttDesc *pasAttDesc;
223 :
224 : char *pszTileName;
225 : int nCoordWidth;
226 : int nZWidth;
227 : int nNTFLevel;
228 :
229 : double dfXYMult;
230 : double dfZMult;
231 :
232 : double dfXOrigin;
233 : double dfYOrigin;
234 :
235 : double dfTileXSize;
236 : double dfTileYSize;
237 :
238 : double dfScale;
239 : double dfPaperToGround;
240 :
241 : long nStartPos;
242 : long nPreSavedPos;
243 : long nPostSavedPos;
244 : NTFRecord *poSavedRecord;
245 :
246 : long nSavedFeatureId;
247 : long nBaseFeatureId;
248 : long nFeatureCount;
249 :
250 : NTFRecord *apoCGroup[MAX_REC_GROUP+1];
251 :
252 : char *pszProduct;
253 : char *pszPVName;
254 : int nProduct;
255 :
256 : void EstablishLayers();
257 :
258 : void ClearCGroup();
259 : void ClearDefs();
260 :
261 : OGRNTFLayer *apoTypeTranslation[100];
262 :
263 : NTFRecordGrouper pfnRecordGrouper;
264 :
265 : int anIndexSize[100];
266 : NTFRecord **apapoRecordIndex[100];
267 : int bIndexBuilt;
268 : int bIndexNeeded;
269 :
270 : void EstablishRasterAccess();
271 : int nRasterXSize;
272 : int nRasterYSize;
273 : int nRasterDataType;
274 : double adfGeoTransform[6];
275 :
276 : OGRNTFRasterLayer *poRasterLayer;
277 :
278 : long *panColumnOffset;
279 :
280 : int bCacheLines;
281 : int nLineCacheSize;
282 : OGRGeometry **papoLineCache;
283 :
284 : public:
285 : NTFFileReader( OGRNTFDataSource * );
286 : ~NTFFileReader();
287 :
288 : int Open( const char * pszFilename = NULL );
289 : void Close();
290 31224 : FILE *GetFP() { return fp; }
291 : void GetFPPos( long *pnPos, long * pnFeatureId);
292 : int SetFPPos( long nPos, long nFeatureId );
293 : void Reset();
294 : void SetBaseFID( long nFeatureId );
295 :
296 :
297 : OGRGeometry *ProcessGeometry( NTFRecord *, int * = NULL );
298 : OGRGeometry *ProcessGeometry3D( NTFRecord *, int * = NULL );
299 : int ProcessAttDesc( NTFRecord *, NTFAttDesc * );
300 : int ProcessAttRec( NTFRecord *, int *, char ***, char ***);
301 : int ProcessAttRecGroup( NTFRecord **, char ***, char ***);
302 :
303 : NTFAttDesc *GetAttDesc( const char * );
304 :
305 : void ApplyAttributeValues( OGRFeature *, NTFRecord **, ... );
306 :
307 : int ApplyAttributeValue( OGRFeature *, int, const char *,
308 : char **, char ** );
309 :
310 : int ProcessAttValue( const char *pszValType,
311 : const char *pszRawValue,
312 : char **ppszAttName,
313 : char **ppszAttValue,
314 : char **ppszCodeDesc );
315 :
316 : int TestForLayer( OGRNTFLayer * );
317 : OGRFeature *ReadOGRFeature( OGRNTFLayer * = NULL );
318 : NTFRecord **ReadRecordGroup();
319 : NTFRecord *ReadRecord();
320 : void SaveRecord( NTFRecord * );
321 :
322 : void DumpReadable( FILE * );
323 :
324 424467 : int GetXYLen() { return nCoordWidth; }
325 217706 : double GetXYMult() { return dfXYMult; }
326 108853 : double GetXOrigin() { return dfXOrigin; }
327 108853 : double GetYOrigin() { return dfYOrigin; }
328 0 : double GetZMult() { return dfZMult; }
329 31218 : const char *GetTileName() { return pszTileName; }
330 0 : const char *GetFilename() { return pszFilename; }
331 0 : int GetNTFLevel() { return nNTFLevel; }
332 0 : const char *GetProduct() { return pszProduct; }
333 : const char *GetPVName() { return pszPVName; }
334 196399 : int GetProductId() { return nProduct; }
335 : double GetScale() { return dfScale; }
336 1342 : double GetPaperToGround() { return dfPaperToGround; }
337 :
338 276 : int GetFCCount() { return nFCCount; }
339 : int GetFeatureClass( int, char **, char ** );
340 :
341 : void OverrideTileName( const char * );
342 :
343 : // Generic file index
344 : void IndexFile();
345 : void FreshenIndex();
346 : void DestroyIndex();
347 : NTFRecord *GetIndexedRecord( int, int );
348 : NTFRecord **GetNextIndexedRecordGroup( NTFRecord ** );
349 :
350 : // Line geometry cache
351 : OGRGeometry *CacheGetByGeomId( int );
352 : void CacheAddByGeomId( int, OGRGeometry * );
353 : void CacheClean();
354 : void CacheLineGeometryInGroup( NTFRecord ** );
355 :
356 : int FormPolygonFromCache( OGRFeature * );
357 :
358 : // just for use of OGRNTFDatasource
359 : void EstablishLayer( const char *, OGRwkbGeometryType,
360 : NTFFeatureTranslator, int,
361 : NTFGenericClass *, ... );
362 :
363 : // Raster related
364 : int IsRasterProduct();
365 0 : int GetRasterXSize() { return nRasterXSize; }
366 0 : int GetRasterYSize() { return nRasterYSize; }
367 : int GetRasterDataType() { return nRasterDataType; }
368 0 : double *GetGeoTransform() { return adfGeoTransform; }
369 : CPLErr ReadRasterColumn( int, float * );
370 :
371 : };
372 :
373 : /************************************************************************/
374 : /* OGRNTFLayer */
375 : /************************************************************************/
376 :
377 : class OGRNTFLayer : public OGRLayer
378 : {
379 : OGRFeatureDefn *poFeatureDefn;
380 : NTFFeatureTranslator pfnTranslator;
381 :
382 : OGRNTFDataSource *poDS;
383 :
384 : int iCurrentReader;
385 : long nCurrentPos;
386 : long nCurrentFID;
387 :
388 : public:
389 : OGRNTFLayer( OGRNTFDataSource * poDS,
390 : OGRFeatureDefn * poFeatureDefine,
391 : NTFFeatureTranslator pfnTranslator );
392 :
393 : ~OGRNTFLayer();
394 :
395 : void ResetReading();
396 : OGRFeature * GetNextFeature();
397 :
398 : #ifdef notdef
399 : OGRFeature *GetFeature( long nFeatureId );
400 : OGRErr SetFeature( OGRFeature *poFeature );
401 : OGRErr CreateFeature( OGRFeature *poFeature );
402 : #endif
403 :
404 62484 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
405 :
406 : #ifdef notdef
407 : int GetFeatureCount( int );
408 : #endif
409 :
410 : int TestCapability( const char * );
411 :
412 : virtual OGRSpatialReference *GetSpatialRef();
413 :
414 : // special to NTF
415 : OGRFeature *FeatureTranslate( NTFFileReader *, NTFRecord ** );
416 : };
417 :
418 : /************************************************************************/
419 : /* OGRNTFFeatureClassLayer */
420 : /************************************************************************/
421 :
422 : class OGRNTFFeatureClassLayer : public OGRLayer
423 : {
424 : OGRFeatureDefn *poFeatureDefn;
425 : OGRGeometry *poFilterGeom;
426 :
427 : OGRNTFDataSource *poDS;
428 :
429 : int iCurrentFC;
430 :
431 : public:
432 : OGRNTFFeatureClassLayer( OGRNTFDataSource * poDS );
433 : ~OGRNTFFeatureClassLayer();
434 :
435 0 : OGRGeometry * GetSpatialFilter() { return poFilterGeom; }
436 : void SetSpatialFilter( OGRGeometry * );
437 :
438 : void ResetReading();
439 : OGRFeature * GetNextFeature();
440 :
441 : OGRFeature *GetFeature( long nFeatureId );
442 :
443 4 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
444 :
445 : int GetFeatureCount( int = TRUE );
446 :
447 : int TestCapability( const char * );
448 : };
449 :
450 : /************************************************************************/
451 : /* OGRNTFRasterLayer */
452 : /************************************************************************/
453 :
454 : class OGRNTFRasterLayer : public OGRLayer
455 : {
456 : OGRFeatureDefn *poFeatureDefn;
457 : OGRGeometry *poFilterGeom;
458 :
459 : OGRNTFDataSource *poDS;
460 :
461 : NTFFileReader *poReader;
462 :
463 : float *pafColumn;
464 : int iColumnOffset;
465 :
466 : int iCurrentFC;
467 :
468 : int nDEMSample;
469 : int nFeatureCount;
470 :
471 : public:
472 : OGRNTFRasterLayer( OGRNTFDataSource * poDS,
473 : NTFFileReader * poReaderIn );
474 : ~OGRNTFRasterLayer();
475 :
476 0 : OGRGeometry * GetSpatialFilter() { return poFilterGeom; }
477 : void SetSpatialFilter( OGRGeometry * );
478 :
479 : void ResetReading();
480 : OGRFeature * GetNextFeature();
481 :
482 : OGRFeature *GetFeature( long nFeatureId );
483 :
484 0 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
485 :
486 : int GetFeatureCount( int = TRUE );
487 :
488 : virtual OGRSpatialReference *GetSpatialRef();
489 :
490 : int TestCapability( const char * );
491 : };
492 :
493 : /************************************************************************/
494 : /* OGRNTFDataSource */
495 : /************************************************************************/
496 :
497 : class OGRNTFDataSource : public OGRDataSource
498 : {
499 : char *pszName;
500 :
501 : int nLayers;
502 : OGRLayer **papoLayers;
503 :
504 : OGRNTFFeatureClassLayer *poFCLayer;
505 :
506 : int iCurrentFC;
507 : int iCurrentReader;
508 : long nCurrentPos;
509 : long nCurrentFID;
510 :
511 : int nNTFFileCount;
512 : NTFFileReader **papoNTFFileReader;
513 :
514 : int nFCCount;
515 : char **papszFCNum;
516 : char **papszFCName;
517 :
518 : OGRSpatialReference *poSpatialRef;
519 :
520 : NTFGenericClass aoGenericClass[100];
521 :
522 : char **papszOptions;
523 :
524 : void EnsureTileNameUnique( NTFFileReader * );
525 :
526 : public:
527 : OGRNTFDataSource();
528 : ~OGRNTFDataSource();
529 :
530 : void SetOptionList( char ** );
531 : const char *GetOption( const char * );
532 :
533 : int Open( const char * pszName, int bTestOpen = FALSE,
534 : char ** papszFileList = NULL );
535 :
536 2 : const char *GetName() { return pszName; }
537 : int GetLayerCount();
538 : OGRLayer *GetLayer( int );
539 : int TestCapability( const char * );
540 :
541 : // Note: these are specific to NTF for now, but eventually might
542 : // might be available as part of a more object oriented approach to
543 : // features like that in FME or SFCORBA.
544 : void ResetReading();
545 : OGRFeature * GetNextFeature();
546 :
547 : // these are only for the use of the NTFFileReader class.
548 : OGRNTFLayer *GetNamedLayer( const char * );
549 : void AddLayer( OGRLayer * );
550 :
551 : // Mainly for OGRNTFLayer class
552 31240 : int GetFileCount() { return nNTFFileCount; }
553 31224 : NTFFileReader *GetFileReader(int i) { return papoNTFFileReader[i]; }
554 :
555 2 : int GetFCCount() { return nFCCount; }
556 : int GetFeatureClass( int, char **, char ** );
557 :
558 19834 : OGRSpatialReference *GetSpatialRef() { return poSpatialRef; }
559 :
560 0 : NTFGenericClass *GetGClass( int i ) { return aoGenericClass + i; }
561 : void WorkupGeneric( NTFFileReader * );
562 : void EstablishGenericLayers();
563 : };
564 :
565 : /************************************************************************/
566 : /* OGRNTFDriver */
567 : /************************************************************************/
568 :
569 : class OGRNTFDriver : public OGRSFDriver
570 64 : {
571 : public:
572 : ~OGRNTFDriver();
573 :
574 : const char *GetName();
575 : OGRDataSource *Open( const char *, int );
576 : int TestCapability( const char * );
577 : };
578 :
579 : /************************************************************************/
580 : /* Support functions. */
581 : /************************************************************************/
582 : int NTFArcCenterFromEdgePoints( double x_c0, double y_c0,
583 : double x_c1, double y_c1,
584 : double x_c2, double y_c2,
585 : double *x_center, double *y_center );
586 : OGRGeometry *
587 : NTFStrokeArcToOGRGeometry_Points( double dfStartX, double dfStartY,
588 : double dfAlongX, double dfAlongY,
589 : double dfEndX, double dfEndY,
590 : int nVertexCount );
591 : OGRGeometry *
592 : NTFStrokeArcToOGRGeometry_Angles( double dfCenterX, double dfCenterY,
593 : double dfRadius,
594 : double dfStartAngle, double dfEndAngle,
595 : int nVertexCount );
596 :
597 : #endif /* ndef _NTF_H_INCLUDED */
|