Giter VIP home page Giter VIP logo

Comments (4)

mbloch avatar mbloch commented on May 27, 2024

I might not be able to help with this... but can you tell me what part of mapshaper you want to use? Do you want to use any parts of the graphics user interface, or just some of the geoprocessing functions?

from mapshaper.

Anqing-None avatar Anqing-None commented on May 27, 2024

I have a array like this:

// x means lng, y means lat
const mydata = [{ x: 0, y: 0, value: 0}, { x: 1, y: 1, value: 1}, ...]

I want to turn it into geojson format:

{
  "type":"FeatureCollection", 
  "features": [
    {
    "type":"Feature",
    "geometry":{"type":"Point", "coordinates":[0, 0]},
    "properties": {value: 0}
    },
    ...
   ]
}

I have read some source code, it use importJSONTable(content, opts);.

How can I pass opts to importJSONTable function to transfer mydata to geojson?

from mapshaper.

mbloch avatar mbloch commented on May 27, 2024

importJSONTable() is an internal function that is not designed to be called directly. I would recommend using the API function applyCommands() (see https://github.com/mbloch/mapshaper/wiki/Using-mapshaper-programmatically#applycommands).

Your code would look something like this:

var cmd = "-i data.json -points x=x y=y -o format=geojson"
var output = await mapshaper.applyCommands(cmd, mydata);
var data = JSON.parse(output["data.json"]);

You can verify that the above command is correct using the mapshaper command line tool or the console in the web UI.

from mapshaper.

ThomasG77 avatar ThomasG77 commented on May 27, 2024

You can also create your own function for the purpose, not attached to mapshaper library in particular

const mydata = [{ x: 0, y: 0, value: 0}, { x: 1, y: 1, value: 1}]

const generateGeoJSONFromArray = data => {
  return {
    "type":"FeatureCollection", 
    "features": data.map(el => {
      const {x, y, ...properties} = el;
      return {
        "type":"Feature",
        "geometry":{
          "type":"Point",
          "coordinates":[x, y]
        },
        "properties": properties
      }
    })
  }
}

console.log(generateGeoJSONFromArray(mydata))

from mapshaper.

Related Issues (20)

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.