0
# Format Support
1
2
Comprehensive format reading and writing system supporting major geospatial data formats including GeoJSON, KML, GPX, and various OGC service formats.
3
4
## Capabilities
5
6
### Base Format Classes
7
8
Foundation classes for different types of format readers and writers.
9
10
```typescript { .api }
11
/**
12
* Base feature format class
13
*/
14
abstract class Feature {
15
constructor();
16
17
/** Read features from source */
18
abstract readFeatures(source: Document | Element | Object | string, options?: ReadOptions): Feature[];
19
/** Read single feature from source */
20
abstract readFeature(source: Document | Element | Object | string, options?: ReadOptions): Feature | null;
21
/** Read geometry from source */
22
abstract readGeometry(source: Document | Element | Object | string, options?: ReadOptions): Geometry | null;
23
/** Read projection from source */
24
abstract readProjection(source: Document | Element | Object | string): Projection | null;
25
26
/** Write features to format */
27
abstract writeFeatures(features: Feature[], options?: WriteOptions): string;
28
/** Write single feature to format */
29
abstract writeFeature(feature: Feature, options?: WriteOptions): string;
30
/** Write geometry to format */
31
abstract writeGeometry(geometry: Geometry, options?: WriteOptions): string;
32
}
33
34
/**
35
* Base JSON feature format class
36
*/
37
abstract class JSONFeature extends Feature {
38
constructor();
39
40
/** Read features from JSON object or string */
41
readFeaturesFromObject(object: Object, options?: ReadOptions): Feature[];
42
/** Write features to JSON object */
43
writeFeaturesObject(features: Feature[], options?: WriteOptions): Object;
44
}
45
46
/**
47
* Base XML feature format class
48
*/
49
abstract class XMLFeature extends Feature {
50
constructor();
51
52
/** Read features from XML node */
53
readFeaturesFromNode(node: Element, options?: ReadOptions): Feature[];
54
/** Write features to XML node */
55
writeFeaturesNode(features: Feature[], options?: WriteOptions): Node;
56
}
57
58
interface ReadOptions {
59
/** Data projection */
60
dataProjection?: ProjectionLike;
61
/** Extent to limit reading */
62
extent?: Extent;
63
/** Feature projection */
64
featureProjection?: ProjectionLike;
65
}
66
67
interface WriteOptions {
68
/** Data projection */
69
dataProjection?: ProjectionLike;
70
/** Feature projection */
71
featureProjection?: ProjectionLike;
72
/** Decimal precision */
73
decimals?: number;
74
/** Right-hand rule for coordinates */
75
rightHanded?: boolean;
76
}
77
```
78
79
### GeoJSON Format
80
81
Standard JSON format for geographic data with full feature support.
82
83
```typescript { .api }
84
/**
85
* GeoJSON format reader/writer
86
* @param options - GeoJSON configuration options
87
*/
88
class GeoJSON extends JSONFeature {
89
constructor(options?: GeoJSONOptions);
90
91
/** Read features from GeoJSON */
92
readFeatures(source: GeoJSONObject | string, options?: ReadOptions): Feature[];
93
/** Read single feature from GeoJSON */
94
readFeature(source: GeoJSONObject | string, options?: ReadOptions): Feature | null;
95
/** Read geometry from GeoJSON */
96
readGeometry(source: GeoJSONObject | string, options?: ReadOptions): Geometry | null;
97
/** Read projection from GeoJSON */
98
readProjection(source: GeoJSONObject | string): Projection | null;
99
100
/** Write features to GeoJSON string */
101
writeFeatures(features: Feature[], options?: WriteOptions): string;
102
/** Write single feature to GeoJSON string */
103
writeFeature(feature: Feature, options?: WriteOptions): string;
104
/** Write geometry to GeoJSON string */
105
writeGeometry(geometry: Geometry, options?: WriteOptions): string;
106
107
/** Write features to GeoJSON object */
108
writeFeaturesObject(features: Feature[], options?: WriteOptions): GeoJSONFeatureCollection;
109
/** Write single feature to GeoJSON object */
110
writeFeatureObject(feature: Feature, options?: WriteOptions): GeoJSONFeature;
111
/** Write geometry to GeoJSON object */
112
writeGeometryObject(geometry: Geometry, options?: WriteOptions): GeoJSONGeometry;
113
}
114
115
interface GeoJSONOptions {
116
/** Data projection */
117
dataProjection?: ProjectionLike;
118
/** Feature projection */
119
featureProjection?: ProjectionLike;
120
/** Extract geometry name */
121
geometryName?: string;
122
}
123
124
interface GeoJSONFeatureCollection {
125
type: 'FeatureCollection';
126
features: GeoJSONFeature[];
127
}
128
129
interface GeoJSONFeature {
130
type: 'Feature';
131
geometry: GeoJSONGeometry | null;
132
properties: Object | null;
133
id?: string | number;
134
}
135
136
interface GeoJSONGeometry {
137
type: string;
138
coordinates: any[];
139
}
140
```
141
142
**Usage Examples:**
143
144
```typescript
145
import GeoJSON from 'ol/format/GeoJSON';
146
import VectorSource from 'ol/source/Vector';
147
148
// Create GeoJSON format
149
const geojsonFormat = new GeoJSON();
150
151
// Read features from GeoJSON
152
const geojsonString = `{
153
"type": "FeatureCollection",
154
"features": [
155
{
156
"type": "Feature",
157
"geometry": {
158
"type": "Point",
159
"coordinates": [0, 0]
160
},
161
"properties": {
162
"name": "Sample Point"
163
}
164
}
165
]
166
}`;
167
168
const features = geojsonFormat.readFeatures(geojsonString, {
169
featureProjection: 'EPSG:3857'
170
});
171
172
// Write features to GeoJSON
173
const geojsonOutput = geojsonFormat.writeFeatures(features, {
174
featureProjection: 'EPSG:3857',
175
dataProjection: 'EPSG:4326'
176
});
177
178
// Use with vector source
179
const vectorSource = new VectorSource({
180
format: new GeoJSON(),
181
url: 'data/features.geojson'
182
});
183
```
184
185
### KML Format
186
187
Keyhole Markup Language format for geographic visualization.
188
189
```typescript { .api }
190
/**
191
* KML format reader/writer
192
* @param options - KML configuration options
193
*/
194
class KML extends XMLFeature {
195
constructor(options?: KMLOptions);
196
197
/** Read features from KML */
198
readFeatures(source: Document | Element | string, options?: ReadOptions): Feature[];
199
/** Read single feature from KML */
200
readFeature(source: Document | Element | string, options?: ReadOptions): Feature | null;
201
/** Read name from KML document */
202
readName(source: Document | Element | string): string | undefined;
203
/** Read network links from KML */
204
readNetworkLinks(source: Document | Element | string, options?: ReadOptions): Object[];
205
/** Read regions from KML */
206
readRegion(source: Document | Element | string): Extent | null;
207
208
/** Write features to KML string */
209
writeFeatures(features: Feature[], options?: WriteOptions): string;
210
/** Write single feature to KML string */
211
writeFeature(feature: Feature, options?: WriteOptions): string;
212
}
213
214
interface KMLOptions {
215
/** Extract styles */
216
extractStyles?: boolean;
217
/** Show point names */
218
showPointNames?: boolean;
219
/** Default style */
220
defaultStyle?: Style[];
221
/** Write styles */
222
writeStyles?: boolean;
223
}
224
```
225
226
**Usage Examples:**
227
228
```typescript
229
import KML from 'ol/format/KML';
230
231
// Create KML format
232
const kmlFormat = new KML({
233
extractStyles: true,
234
showPointNames: true
235
});
236
237
// Read features from KML file
238
fetch('data/places.kml')
239
.then(response => response.text())
240
.then(kmlText => {
241
const features = kmlFormat.readFeatures(kmlText, {
242
featureProjection: 'EPSG:3857'
243
});
244
vectorSource.addFeatures(features);
245
});
246
247
// Write features to KML
248
const kmlOutput = kmlFormat.writeFeatures(features, {
249
featureProjection: 'EPSG:3857',
250
dataProjection: 'EPSG:4326'
251
});
252
```
253
254
### GPX Format
255
256
GPS Exchange Format for GPS track and waypoint data.
257
258
```typescript { .api }
259
/**
260
* GPX format reader/writer for GPS data
261
* @param options - GPX configuration options
262
*/
263
class GPX extends XMLFeature {
264
constructor(options?: GPXOptions);
265
266
/** Read features from GPX */
267
readFeatures(source: Document | Element | string, options?: ReadOptions): Feature[];
268
/** Read projection from GPX (always EPSG:4326) */
269
readProjection(source: Document | Element | string): Projection;
270
271
/** Write features to GPX string */
272
writeFeatures(features: Feature[], options?: WriteOptions): string;
273
}
274
275
interface GPXOptions {
276
/** Read track extensions */
277
readExtensions?: (feature: Feature, node: Element) => void;
278
}
279
```
280
281
### Vector Tile Formats
282
283
Formats for handling vector tile data.
284
285
```typescript { .api }
286
/**
287
* Mapbox Vector Tiles (MVT) format
288
* @param options - MVT configuration options
289
*/
290
class MVT extends Feature {
291
constructor(options?: MVTOptions);
292
293
/** Read features from MVT buffer */
294
readFeatures(source: ArrayBuffer, options?: ReadOptions): Feature[];
295
/** Read projection (always EPSG:3857) */
296
readProjection(): Projection;
297
298
/** Set layer names to read */
299
setLayers(layers: string[]): void;
300
}
301
302
interface MVTOptions {
303
/** Feature class to use */
304
featureClass?: typeof Feature;
305
/** Geometry name */
306
geometryName?: string;
307
/** Layer names to read */
308
layers?: string[];
309
/** ID property name */
310
idProperty?: string;
311
}
312
```
313
314
### Well-Known Text Formats
315
316
Text-based geometry formats for data exchange.
317
318
```typescript { .api }
319
/**
320
* Well-Known Text (WKT) format for geometries
321
* @param options - WKT configuration options
322
*/
323
class WKT extends TextFeature {
324
constructor(options?: WKTOptions);
325
326
/** Read geometry from WKT string */
327
readGeometry(source: string, options?: ReadOptions): Geometry;
328
/** Read feature from WKT string */
329
readFeature(source: string, options?: ReadOptions): Feature;
330
/** Read features from WKT string */
331
readFeatures(source: string, options?: ReadOptions): Feature[];
332
333
/** Write geometry to WKT string */
334
writeGeometry(geometry: Geometry, options?: WriteOptions): string;
335
/** Write feature to WKT string */
336
writeFeature(feature: Feature, options?: WriteOptions): string;
337
/** Write features to WKT string */
338
writeFeatures(features: Feature[], options?: WriteOptions): string;
339
}
340
341
/**
342
* Well-Known Binary (WKB) format for geometries
343
*/
344
class WKB extends Feature {
345
constructor();
346
347
/** Read geometry from WKB buffer */
348
readGeometry(source: ArrayBuffer, options?: ReadOptions): Geometry;
349
/** Read feature from WKB buffer */
350
readFeature(source: ArrayBuffer, options?: ReadOptions): Feature;
351
352
/** Write geometry to WKB buffer */
353
writeGeometry(geometry: Geometry, options?: WriteOptions): ArrayBuffer;
354
/** Write feature to WKB buffer */
355
writeFeature(feature: Feature, options?: WriteOptions): ArrayBuffer;
356
}
357
```
358
359
**Usage Examples:**
360
361
```typescript
362
import { WKT, WKB } from 'ol/format';
363
import Point from 'ol/geom/Point';
364
365
// WKT format
366
const wktFormat = new WKT();
367
368
// Read geometry from WKT
369
const wktString = 'POINT(0 0)';
370
const geometry = wktFormat.readGeometry(wktString);
371
372
// Write geometry to WKT
373
const point = new Point([100, 200]);
374
const wktOutput = wktFormat.writeGeometry(point);
375
// Output: "POINT(100 200)"
376
377
// WKB format
378
const wkbFormat = new WKB();
379
const wkbBuffer = wkbFormat.writeGeometry(point);
380
const geometryFromWkb = wkbFormat.readGeometry(wkbBuffer);
381
```
382
383
### Service Formats
384
385
Formats for OGC web service responses and capabilities.
386
387
```typescript { .api }
388
/**
389
* WMS Capabilities format reader
390
*/
391
class WMSCapabilities extends XML {
392
constructor();
393
394
/** Read WMS capabilities */
395
read(source: Document | Element | string): Object;
396
}
397
398
/**
399
* WMTS Capabilities format reader
400
*/
401
class WMTSCapabilities extends XML {
402
constructor();
403
404
/** Read WMTS capabilities */
405
read(source: Document | Element | string): Object;
406
}
407
408
/**
409
* WFS (Web Feature Service) format
410
* @param options - WFS configuration options
411
*/
412
class WFS extends XMLFeature {
413
constructor(options?: WFSOptions);
414
415
/** Read features from WFS response */
416
readFeatures(source: Document | Element | string, options?: ReadOptions): Feature[];
417
/** Read transaction response */
418
readTransactionResponse(source: Document | Element | string): Object;
419
420
/** Write GetFeature request */
421
writeGetFeature(options: WFSWriteGetFeatureOptions): Node;
422
/** Write Transaction request */
423
writeTransaction(inserts: Feature[], updates: Feature[], deletes: Feature[], options: WFSWriteTransactionOptions): Node;
424
}
425
426
interface WFSOptions {
427
/** Feature namespace URI */
428
featureNS?: string;
429
/** Feature type */
430
featureType?: string | string[];
431
/** GML format */
432
gmlFormat?: GMLBase;
433
/** Schema location */
434
schemaLocation?: string;
435
}
436
437
/**
438
* ESRI JSON format for ArcGIS services
439
*/
440
class EsriJSON extends JSONFeature {
441
constructor();
442
443
/** Read features from ESRI JSON */
444
readFeatures(source: Object | string, options?: ReadOptions): Feature[];
445
/** Read geometry from ESRI JSON */
446
readGeometry(source: Object | string, options?: ReadOptions): Geometry;
447
448
/** Write features to ESRI JSON */
449
writeGeometry(geometry: Geometry, options?: WriteOptions): Object;
450
}
451
```
452
453
### Polyline Encoding
454
455
Google Polyline Algorithm for encoding coordinate paths.
456
457
```typescript { .api }
458
/**
459
* Google Polyline encoding format
460
* @param options - Polyline configuration options
461
*/
462
class Polyline extends TextFeature {
463
constructor(options?: PolylineOptions);
464
465
/** Read geometry from encoded polyline string */
466
readGeometry(source: string, options?: ReadOptions): LineString;
467
/** Read feature from encoded polyline string */
468
readFeature(source: string, options?: ReadOptions): Feature;
469
470
/** Write geometry to encoded polyline string */
471
writeGeometry(geometry: Geometry, options?: WriteOptions): string;
472
}
473
474
interface PolylineOptions {
475
/** Encoding factor (default: 1e5) */
476
factor?: number;
477
/** Geometry layout */
478
geometryLayout?: GeometryLayout;
479
}
480
```
481
482
**Usage Examples:**
483
484
```typescript
485
import Polyline from 'ol/format/Polyline';
486
import LineString from 'ol/geom/LineString';
487
488
// Create polyline format
489
const polylineFormat = new Polyline();
490
491
// Decode polyline string
492
const encodedPolyline = 'u{~vFvyys@fS]';
493
const lineString = polylineFormat.readGeometry(encodedPolyline, {
494
dataProjection: 'EPSG:4326',
495
featureProjection: 'EPSG:3857'
496
});
497
498
// Encode linestring to polyline
499
const coordinates = [[-120, 38], [-121, 39], [-122, 40]];
500
const geometry = new LineString(coordinates);
501
const encoded = polylineFormat.writeGeometry(geometry);
502
```
503
504
### TopoJSON Format
505
506
Topological JSON format for efficient geographic data encoding.
507
508
```typescript { .api }
509
/**
510
* TopoJSON format reader
511
* @param options - TopoJSON configuration options
512
*/
513
class TopoJSON extends JSONFeature {
514
constructor(options?: TopoJSONOptions);
515
516
/** Read features from TopoJSON */
517
readFeatures(source: Object | string, options?: ReadOptions): Feature[];
518
/** Read projection from TopoJSON */
519
readProjection(source: Object | string): Projection | null;
520
}
521
522
interface TopoJSONOptions {
523
/** Layer names to read */
524
layers?: string[];
525
}
526
```
527
528
## Types
529
530
```typescript { .api }
531
type GeoJSONObject = GeoJSONGeometry | GeoJSONFeature | GeoJSONFeatureCollection;
532
type GeometryLayout = 'XY' | 'XYZ' | 'XYM' | 'XYZM';
533
```