GithubHelp home page GithubHelp logo

mapbox / tokml Goto Github PK

View Code? Open in Web Editor NEW
185.0 110.0 90.0 97 KB

Convert GeoJSON to KML.

Home Page: http://mapbox.github.io/tokml/

License: BSD 2-Clause "Simplified" License

HTML 1.78% JavaScript 98.22%

tokml's Introduction

Build Status Coverage Status

tokml

Convert GeoJSON to KML.

Usage

with node/browserify

npm install --save tokml

otherwise:

wget https://raw.github.com/mapbox/tokml/master/tokml.js

as a binary:

npm install -g tokml
tokml file.geojson > file.kml
tokml < file.geojson > file.kml

Example

// kml is a string of KML data, geojsonObject is a JavaScript object of
// GeoJSON data
var kml = tokml(geojsonObject);

// grab name and description properties from each object and write them in
// KML
var kmlNameDescription = tokml(geojsonObject, {
    name: 'name',
    description: 'description'
});

// name and describe the KML document as a whole
var kmlDocumentName = tokml(geojsonObject, {
    documentName: 'My List Of Markers',
    documentDescription: "One of the many places you are not I am"
});

API

tokml(geojsonObject, [options])

Given GeoJSON data as an object, return KML data as a string of XML.

options is an optional object that takes the following options:

The property to name/description mapping: while GeoJSON supports freeform properties on each feature, KML has an expectation of name and description properties that are often styled and displayed automatically. These options let you define a mapping from the GeoJSON style to KML's.

  • name: the name of the property in each GeoJSON Feature that contains the feature's name
  • description: the name of the property in each GeoJSON Feature that contains the feature's description

Timestamp: KML can associate features with a moment in time via the TimeStamp tag. GeoJSON doesn't have a comparable field, but a custom property can be mapped

  • timestamp: the name of the property in each GeoJSON Feature that contains a timestamp in XML Schema Time (yyyy-mm-ddThh:mm:sszzzzzz)

Document name and description: KML supports name and description properties for the full document.

  • documentName: the name of the full document
  • documentDescription: the description of the full document

simplestyle-spec support:

  • simplestyle: set to true to convert simplestyle-spec styles into KML styles

Development

Requires node.js and browserify:

To build tokml.js:

make

To run tests:

npm install
npm test

tokml's People

Contributors

chopj avatar jfirebaugh avatar kellybigley avatar tmcw avatar tyrasd avatar vincentsels avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tokml's Issues

cds.join is not a function error

TypeError: cds.join is not a function
    at index.js:173:1
    at Array.map (<anonymous>)
    at linearring (index.js:173:1)
    at Object.Polygon (index.js:115:1)
    at Object.any (index.js:153:1)
    at index.js:28:1
    at Array.map (<anonymous>)
    at root (index.js:71:1)
    at tokml (index.js:16:1)
    at KmlBtn (kmlBtn.js:19:1)

Geoson polygons with no inner rings

If there are no inner rings,
TypeError: Cannot call method 'map' of undefined
../tokml/index.js:130:14
../tokml/index.js:90:54

Proposed Fix:
Polygon: function(_) {
var outer = _.coordinates[0],
inner = _.coordinates.slice(1),

        outerRing = outer ? tag('outerBoundaryIs',
            tag('LinearRing', tag('coordinates', linearring(outer)))) : '',

        innerRings = inner ? inner.map(function(i) {
            return tag('innerBoundaryIs',
                tag('LinearRing', tag('coordinates', linearring(i))));
        }).join('') : '';

    return tag('Polygon', outerRing + innerRings);
}

Broken icon link

Hello. When converting a point geometry to KML, toKML adds the following style:

<Style id="mcwhitemsmediums27b2cfsw1mo1ffffffffo04">
            <IconStyle>
                <Icon>
                    <href>https://api.tiles.mapbox.com/v3/marker/pin-m+white.png</href>
                </Icon>
            </IconStyle>
            <hotSpot xunits="fraction" yunits="fraction" x="0.5" y="0.5"></hotSpot>
        </Style>

But the icon link https://api.tiles.mapbox.com/v3/marker/pin-m+white.png is broken.

If I try to access the icon link directly, I get the message:
{"message":"Invalid marker: pin-m+white(0,0)"}

If I upload the KML file to Google Earth, it shows a faulty (red-X) icon.
image

id ignored

I'd expect tokml to take the id property from a GeoJSON Feature and use for the id attribute of the <Placemark>(https://developers.google.com/kml/documentation/kmlreference#placemark).

Repro

const tokml = require('tokml')
const geojson = { type: 'Feature', id: 'abc', properties: {}, geometry: {type: 'Point', coordinates: [1,2]}};
tokml(geojson)

Actual

<?xml version="1.0" encoding="UTF-8"?>
<kml
  xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark>
      <ExtendedData></ExtendedData>
      <Point>
        <coordinates>1,2</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

Expected

<?xml version="1.0" encoding="UTF-8"?>
<kml
  xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark id="abc">
      <ExtendedData></ExtendedData>
      <Point>
        <coordinates>1,2</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

escaping cdata?

Great module!

I'm converting kml which contains cdata to geojson and saving that back to kml. In the process, the cdata tags are lost, which makes the xml invalid. I resolved this issue by changing the description function to wrap the description in a cdata tag. I'd try to push it, but I'm not sure if it would goof anything up.

Anywho, thanks!

"value" argument is out of bound

buffer.js:1185
throw new TypeError('"value" argument is out of bounds');
^

TypeError: "value" argument is out of bounds
at checkInt (buffer.js:1185:11)
at Buffer.writeUInt8 (buffer.js:1233:5)
at Dbf.generateHeader (/usr/src/app/node_modules/geojson2shp/lib/shp-write-stream/dbf.js:184:11)
at err (/usr/src/app/node_modules/geojson2shp/lib/shp-write-stream/index.js:138:37)
at /usr/src/app/node_modules/pump/index.js:75:7
at f (/usr/src/app/node_modules/once/once.js:25:25)
at WriteStream. (/usr/src/app/node_modules/pump/index.js:33:5)
at WriteStream.f (/usr/src/app/node_modules/once/once.js:25:25)
at WriteStream.onfinish (/usr/src/app/node_modules/end-of-stream/index.js:31:27)
at emitNone (events.js:111:20)

Feature suggestion: style function

The resulting kml is without style, which isn't surprising - GeoJson doesn't inherently contain any style information either. However, Google Maps (and probably pretty much any maps library which supports GeoJson) has a way to give each feature a style, based on some properties. In the case of Google Maps, you can supply a single 'styling function', which is executed for every feature that's displayed on the map. Could we introduce a similar concept in this library? A styling function which accepts a feature and should return e.g. a json representation of the kml Style element? In its most basic form, this could generate inline style. Another option would be to work with style references, where a function determines which (static) style should be used for the specified feature, and then an map containing the definition of each of these (static) styles (still in json form). A combination of both would be ideal, so that a combination of both static as well as dynamic styles (depending on the feature's properties) is possible.

Any thoughts? If you agree, I'll be happy to give it a shot.

CDN js?

Hi, I want to run this from a google apps script. I can't do it the node way there.. will need to do a traditional <script src="path/to/js"></script> . Any leads? I'm not interested in the additional file generation et al, just being able to convert a geojson string to a kml string would be great!

how to install? newbies questions

HI Tom,

I download file as zip file, and installed the note.js. But, still have zero clue about the Usage section you mentioned.

with node, how to install? If you don't mind, could you please provide step by step instructions?

Really Appreciated

with node/browserify

npm install --save tokml
otherwise:

wget https://raw.github.com/mapbox/tokml/master/tokml.js
as a binary:

npm install -g tokml
tokml file.geojson > file.kml
tokml < file.geojson > file.kml

Vulnerability with dependencies

I'm getting these problems when suing the module. Not sure if it can be fixed by just updating to the latest modules or that would break something.

Manual Review
Some vulnerabilities require your attention to resolve

      Visit https://go.npm.me/audit-guide for additional guidance

High Regular Expression Denial of Service

Package minimatch

Patched in >=3.0.2

Dependency of tokml

Path tokml > strxml > tap > glob > minimatch

More info https://nodesecurity.io/advisories/118

Low Incorrect Handling of Non-Boolean Comparisons During
Minification

Package uglify-js

Patched in >= 2.4.24

Dependency of tokml

Path tokml > strxml > tap > runforcover > bunker > burrito >
uglify-js

More info https://nodesecurity.io/advisories/39

Low Regular Expression Denial of Service

Package uglify-js

Patched in >=2.6.0

Dependency of tokml

Path tokml > strxml > tap > runforcover > bunker > burrito >
uglify-js

More info https://nodesecurity.io/advisories/48

found 3 vulnerabilities (2 low, 1 high) in 3335 scanned packages
3 vulnerabilities require manual review. See the full report for details

FeatureCollection Geojson leaflet Layergroup

Hi,

I am new to this but I have been trying to convert a geojson object to kml... I have the string converted to a json object, but somehow, tokml never sees it...? But if the json obj I pass in is a single point or line, it works... here is a sample data

"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-80.236331,25.917525],[-80.236329,25.917435]]}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-80.236329,25.917435],[-80.236327,25.917346]]}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-80.236327,25.917346],[-80.236324,25.917256]]}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-80.236324,25.917256],[-80.236322,25.917167]]}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-80.236322,25.917167],[-80.23632,25.917077]]}}]}"

Thank you in advance!!!

Further Documentation/examples?

Hi all,

Is there any more documentation or examples available online? I am still quite new to Leaflet so I could do with all the help I can get. I currently have a map using Leaflet.Draw with some export feature code. However, the featureGroup is saved as a geoJSON and it would be ideal if this could be converted to .kml before downloading.

The export code is as follows:

       `document.getElementById('export').onclick = function(e) {
        // Extract GeoJson from featureGroup
        var data = featureGroup.toGeoJSON();

        // Stringify the GeoJson
        var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));

        // Create export
        document.getElementById('export').setAttribute('href', 'data:' + convertedData);
        document.getElementById('export').setAttribute('download','data.geojson');
        }`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.