maphubs / tokml

Convert GeoJSON to KML.
BSD 2-Clause "Simplified" License
13 stars 11 forks source link

Uncaught TypeError: string.replace is not a function #4

Closed JuanIrache closed 2 years ago

JuanIrache commented 3 years ago

Hi,

Thanks for working on this. I just migrated from the Mapbox version and my geoJSON is failing to process with

TypeError: string.replace is not a function at escape (...\node_modules\xml-escape\index.js:11:17) at timestamp (...\node_modules\@maphubs\tokml\index.js:103:64) at ...\node_modules\@maphubs\tokml\index.js:61:13

This was fine with the Mapbox version.

Here's a minimal geoJSON that fails to process:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "source": "dji-srt-parser",
        "timestamp": [
          "2017-07-29T05:48:50.000Z",
          "2017-07-29T05:48:51.000Z"
        ],
        "name": "sample2",
        "HOME_LATITUDE": -25.2088,
        "HOME_LONGITUDE": 131.166,
        "ISO": 100,
        "SHUTTER": 60,
        "FNUM": 2.2
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [131.16599999999997, -25.2088, 497.9759216308594],
          [131.16599999999997, -25.2088, 497.9759216308594]
        ]
      }
    }
  ]
}
kriscarle commented 3 years ago

This is working fine for me, can you provide a more complete example?

~/dev/misc/tokml-test
❯ more package.json 
{
  "name": "tokml-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@maphubs/tokml": "^0.5.2"
  }
}

~/dev/misc/tokml-test 29s
❯ more package.json 
{
  "name": "tokml-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@maphubs/tokml": "^0.5.2"
  }
}

~/dev/misc/tokml-test
❯ more issue4.geojson 
{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "source": "dji-srt-parser",
        "timestamp": [
          "2017-07-29T05:48:50.000Z",
          "2017-07-29T05:48:51.000Z"
        ],
        "name": "sample2",
        "HOME_LATITUDE": -25.2088,
        "HOME_LONGITUDE": 131.166,
        "ISO": 100,
        "SHUTTER": 60,
        "FNUM": 2.2
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [131.16599999999997, -25.2088, 497.9759216308594],
          [131.16599999999997, -25.2088, 497.9759216308594]
        ]
      }
    }
  ]
}

~/dev/misc/tokml-test
❯ yarn run tokml < ./issue4.geojson

yarn run v1.22.10
$ /Users/kris/dev/misc/tokml-test/node_modules/.bin/tokml
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Placemark><ExtendedData><Data name="source"><value>dji-srt-parser</value></Data><Data name="timestamp"><value>2017-07-29T05:48:50.000Z,2017-07-29T05:48:51.000Z</value></Data><Data name="name"><value>sample2</value></Data><Data name="HOME_LATITUDE"><value>-25.2088</value></Data><Data name="HOME_LONGITUDE"><value>131.166</value></Data><Data name="ISO"><value>100</value></Data><Data name="SHUTTER"><value>60</value></Data><Data name="FNUM"><value>2.2</value></Data></ExtendedData><LineString><coordinates>131.16599999999997,-25.2088,497.9759216308594 131.16599999999997,-25.208✨  Done in 0.40s.
quanticle commented 3 years ago

In my case, the problem occurs when a Feature inside a FeatureCollection has an integer id. The code on line 64 of index.js assigns _.id to attributes.id. Then, in strxml.js, in attr, the code calls esc(attributes[key]). This passes the integer ID to esc, which attempts to call .replace on a non-string object. The following version of linestring.geojson reproduces the issue:

{ "type": "FeatureCollection",
  "features": [{
    "id": 1,
    "type": "Feature",
    "geometry": { "type": "LineString",
    "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
    },
    "properties": {
      "prop0": "value0",
      "prop1": "val2"
      }
    }
  ]
}
quanticle commented 3 years ago

The Mapbox version of this code doesn't set the id attribute, and so does not exhibit this issue.