npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

terriajs-ogr2ogr

v1.3.0

Published

ogr2ogr wrapper w/ multiple format support

Downloads

128

Readme

Build Status NPM NPM Downloads code-style

ogr2ogr enables spatial file conversion and reprojection of spatial data through the use of ogr2ogr (gdal) tool

Requirements

ogr2ogr requires the command line tool ogr2ogr - gdal install page. It is recommended to use the latest version.

Installation

npm install ogr2ogr

Usage

ogr2ogr takes either a path, a stream, or a GeoJSON object. The result of the transformation can be consumed via callback or stream:

var ogr2ogr = require('ogr2ogr')
var ogr = ogr2ogr('/path/to/spatial/file')

ogr.exec(function (er, data) {
  if (er) console.error(er)
  console.log(data)
})

var ogr2 = ogr2ogr('/path/to/another/spatial/file')
ogr2.stream().pipe(writeStream)

See /examples for usage examples and /test/api.js.

Formats

The goal is for ogr2ogr to support most (if not all) formats your underlying ogr2ogr supports. You can see the progress of that in /tests/drivers.js.

It also will:

  1. Extract zip files for formats that are typically bundled (i.e. shapefiles, kmz, s57, vrt, etc)
  2. Will extract geometry from CSVs when a common geometry field can be determined.
  3. Cleans up after its messes.
  4. Bundles multi-file conversions as a zip
  5. Support GeoJSON and GeoRSS urls as path inputs
  6. Support raw GeoJSON objects as input

Options

ogr2ogr takes chainable modifier functions:

var shapefile = ogr2ogr('/path/to/spatial/file.geojson')
                    .format('ESRI Shapefile')
                    .skipfailures()
                    .stream()
shapefile.pipe(fs.createWriteStream('/shapefile.zip'))

Available options include:

  • .project(dest, src) - reproject data (defaults to: "ESPG:4326")
  • .format(fmt) - set output format (defaults to: "GeoJSON")
  • .timeout(ms) - milliseconds before ogr2ogr is killed (defaults to: 15000)
  • .skipfailures() - skip failures (continue after failure, skipping failed feature -- by default failures are not skipped)
  • .options(arr) - array of custom org2ogr arguments (e.g. ['-fieldmap', '2,-1,4'])
  • .destination(str) - ogr2ogr destination (directly tell ogr2ogr where the output should go, useful for writing to databases)
  • .onStderr(callback) - execute a callback function whose parameter is the debug output of ogr2ogr to stderr

Example of onStderr usage

If you want to debug what is the ogr2ogr binary doing internally, you can attach a callback to the output, provided you have passed the option CPL_DEBUG

var shapefile = ogr2ogr('/path/to/spatial/file.geojson')
                    .format('ESRI Shapefile')
                    .skipfailures()
                    .options(["--config", "CPL_DEBUG", "ON"])
			        .onStderr(function(data) {
			            console.log(data);
			        })
                    .stream()
shapefile.pipe(fs.createWriteStream('/shapefile.zip'))

You will see in the console someting in the likes of

GDAL: GDALOpen(/tmp/ogr_542cb61092c/sample.shp, this=0x15ca370) succeeds as ESRI Shapefile.

GDAL: GDALDriver::Create(PGDUMP,/vsistdout/,0,0,0,Unknown,(nil))
PGDump: LaunderName('ID') -> 'id'
PGDump: LaunderName('FIPSSTCO') -> 'fipsstco'
PGDump: LaunderName('STATE') -> 'state'
PGDump: LaunderName('COUNTY') -> 'county'
GDALVectorTranslate: 1 features written in layer 'sample'
Shape: 1 features read on layer 'sample'.
GDAL: GDALClose(/tmp/ogr_542cb61092c/sample.shp, this=0x15ca370)
GDAL: GDALClose(/vsistdout/, this=0x157ded0)

This can be useful when something goes wrong and the error provided by this library doesn't provide enough information.

Conversion of shp files

It is trivial to handle the conversion of ESRI Shapefiles when they are packed in a zipfile that contains (at least) the shp and shx files. This library is also capable of converting uncompresses ESRI Shapefiles if you use the shp file as the input file and the shx file is in the same folder.

However, it is also possible to convert single shp files that lack an shx file by forcing its creation using ogr2ogr option SHAPE_RESTORE_SHX provided you have installed GDAL/OGR version 2.1.0 or newer.

var geojson = ogr2ogr('/path/to/spatial/lonely.shp')
    .options(["--config", "SHAPE_RESTORE_SHX", "TRUE"])
    .stream()

geojson.pipe(fs.createWriteStream('/lonely.json'))

Caveat: ogr2ogr will do its best to infer the corresponding shx. However, there's no guarantee it will success.

License

(The MIT License)

Copyright (c) 2017 Marc Harter [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.