1 | ![Build Status](https://github.com/wavded/ogr2ogr/workflows/build/badge.svg?branch=master) [![NPM](https://img.shields.io/npm/v/ogr2ogr.svg)](https://npmjs.com/package/ogr2ogr) ![NPM Downloads](https://img.shields.io/npm/dt/ogr2ogr.svg)
|
2 |
|
3 | Looking for V2 documentation? [Click here][9].
|
4 |
|
5 | ogr2ogr wraps the `ogr2ogr` GDAL tool to enable file conversion and re-projection of spatial data in simplified friendly API.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | 1. [Install GDAL tools][1] (includes the `ogr2ogr` command line tool)
|
10 |
|
11 | 2. Install package:
|
12 |
|
13 | ```sh
|
14 | npm install ogr2ogr
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ogr2ogr takes either a path, a stream, or a [GeoJSON][2] object. The result of the transformation will depend on the format returned.
|
20 |
|
21 | ```javascript
|
22 | // Using CommonJS modules
|
23 | const ogr2ogr = require('ogr2ogr').default
|
24 | // Using ECMAScript modules or Typescript
|
25 | import ogr2ogr from 'ogr2ogr'
|
26 |
|
27 | // Promise API
|
28 | (async() {
|
29 | // Convert path to GeoJSON.
|
30 | let {data} = await ogr2ogr('/path/to/spatial/file')
|
31 | console.log(data)
|
32 |
|
33 | // Convert GeoJSON object to ESRI Shapefile stream.
|
34 | let {stream} = await ogr2ogr(data, {format: 'ESRI Shapefile'})
|
35 |
|
36 | // Convert ESRI Shapefile stream to KML text.
|
37 | let {text} = await ogr2ogr(stream, {format: 'KML'})
|
38 | console.log(text)
|
39 | })()
|
40 |
|
41 | // Callback API
|
42 | ogr2ogr('/path/to/spatial/file').exec((err, {data}) => {
|
43 | console.log(data)
|
44 | })
|
45 | ```
|
46 |
|
47 | ## Formats
|
48 |
|
49 | ogr2ogr has varying support for format input and output. Consult the particular [driver][3] you are interested in for more details. It is highly recommend to run the latest version of GDAL to get the best support. This project attempts to cast the widest net for support. Here are some notables:
|
50 |
|
51 | | Drivers | Output | Notes |
|
52 | | ----------------------------------------------------- | -------- | ------------------------------------------- |
|
53 | | GeoJSON | `data` | Default format returned when none specified |
|
54 | | CSV, GeoRSS, GML, GMT, GPX, JML, KML, MapML, PDF, VDV | `text` | Drivers supporting `/vsidout/` return text |
|
55 | | Other | `stream` | All other drivers return a file stream |
|
56 |
|
57 | ## API
|
58 |
|
59 | ### ogr2ogr(input, options?) -> Promise\<output\>
|
60 |
|
61 | The **`input`** may be one of:
|
62 |
|
63 | - A path (`string`). This includes file paths and network paths including HTTP endpoints.
|
64 | - A `ReadableStream`.
|
65 | - A [GeoJSON][2] object.
|
66 |
|
67 | The following **`options`** are available (none required):
|
68 |
|
69 | - `format` - Output format (default: `GeoJSON`)
|
70 | - `timeout` - Timeout, in milliseconds, before command forcibly terminated (default: `0`)
|
71 | - `maxBuffer` - Max output size in bytes for stdout/stderr (default: `1024 * 1024 * 50`)
|
72 | - `options` - Custom [ogr2ogr arguments][4] and [driver options][5] (e.g. `['--config', 'SHAPE_RESTORE_SHX', 'TRUE']`)
|
73 | - `env` - Custom environmental variables (e.g. `{ATTRIBUTES_SKIP: 'YES'}`)
|
74 | - `destination` - Select another output than the **output** object (e.g. useful for writing to databases).
|
75 | - `command` - Command to run (default: `ogr2ogr`)
|
76 |
|
77 | The **`output`** object has the following properties:
|
78 |
|
79 | - `cmd` - The `ogr2ogr` command executed (useful for debugging).
|
80 | - `text` - Text output from [drivers][3] that support `/vsistdout/` (see [formats](#formats) above)
|
81 | - `data` - Parsed [GeoJSON][2] output (used when `format` is `GeoJSON`)
|
82 | - `stream` - A `ReadableStream` of the output. Used for [drivers][3] that do not support `/vsistdout/`.
|
83 | - If a driver generates more than one file (like `ESRI Shapefile`), this will be a zip stream containing all the data.
|
84 | - `extname` - The file extension of the data returned.
|
85 | - `details` - Any text printed to `STDERR`. This includes any warnings reported by ogr2ogr when it ran.
|
86 |
|
87 | ### ogr2ogr(input, options?).exec((err, output))
|
88 |
|
89 | The callback API supports the same options as above but in a NodeJS style callback format.
|
90 |
|
91 | ### ogr2ogr.version() -> Promise\<string\>
|
92 |
|
93 | Retrieve the version of `ogr2ogr` that will be called by default by this library (same as calling `ogr2ogr --version` from command line).
|
94 |
|
95 | ```javascript
|
96 | const version = await ogr2ogr.version()
|
97 | console.log(version)
|
98 |
|
99 | // GDAL X.X.X, released XXXX/XX/XX
|
100 | ```
|
101 |
|
102 | ## Tips and tricks
|
103 |
|
104 | Running `ogr2ogr` in a [Docker container][6]:
|
105 |
|
106 | ```javascript
|
107 | ogr2ogr("/home/.../path/to/spatial/file", {
|
108 | command: "docker run -v /home/:/home --rm osgeo/gdal ogr2ogr",
|
109 | })
|
110 | ```
|
111 |
|
112 | Converting an isolated `.shp` file:
|
113 |
|
114 | ```javascript
|
115 | ogr2ogr("/path/to/file.shp", {
|
116 | options: ["--config", "SHAPE_RESTORE_SHX", "TRUE"],
|
117 | })
|
118 | ```
|
119 |
|
120 | Getting more debug information by using the [`CPL_DEBUG`][7] option. Debug info added to `details` on the **`output`** object.
|
121 |
|
122 | ```javascript
|
123 | ogr2ogr("/path/to/file.shp", {
|
124 | options: ["--config", "CPL_DEBUG", "TRUE"],
|
125 | })
|
126 | ```
|
127 |
|
128 | Parsing custom geometry fields in a CSV. Use [CSV driver options][8], like:
|
129 |
|
130 | ```javascript
|
131 | ogr2ogr("/path/to/file.csv", {
|
132 | options: ["-oo", "GEOM_POSSIBLE_NAMES=the_geom"],
|
133 | })
|
134 | ```
|
135 |
|
136 | Re-project geometry:
|
137 |
|
138 | ```javascript
|
139 | ogr2ogr("/path/to/file.shp", {
|
140 | options: ["-t_srs", "EPSG:4326"],
|
141 | })
|
142 | ```
|
143 |
|
144 | [1]: https://gdal.org/download.html
|
145 | [2]: https://geojson.org
|
146 | [3]: https://gdal.org/drivers/vector/index.html
|
147 | [4]: https://gdal.org/programs/ogr2ogr.html
|
148 | [5]: https://gdal.org/drivers/vector/csv.html#open-options
|
149 | [6]: https://github.com/OSGeo/gdal/tree/master/gdal/docker
|
150 | [7]: https://trac.osgeo.org/gdal/wiki/ConfigOptions#CPL_DEBUG
|
151 | [8]: https://gdal.org/drivers/vector/csv.html#open-options
|
152 | [9]: https://github.com/wavded/ogr2ogr/tree/v2
|