# Load Spatial Data

jsgeoda supports reading [geojson](https://geojson.org/) file. The file could be fed using URL or File object   (e.g. via drag-and-drop). The content of the geojson file needs to be read in binary format: [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). Then, you can call `read_geojson()` function to load geojson into jsgeoda.

{% tabs %}
{% tab title="Node.js" %}

```javascript
const jsgeoda = require('jsgeoda');
const fs = require('fs');

// create jsgeoda instance
const geoda = await jsgeoda.New();

// print out "Hello jsgeoda 0.0.8"
console.log("Hello jsgeoda", geoda.version);

// read a geojson file into ArrayBuffer using fs
const data = fs.readFileSync('./data/natregimes.geojson').buffer;

// load geojson in jsgeoda, an unique id (string) will be returned for further usage
const nat_uid = geoda.readGeoJSON(data);

const num_obs = geoda.getNumObs(nat_uid);
console.log("number of observations:", num_obs);

const col_names = geoda.getColNames(nat);
console.log("column names:", col_names);

const hr60 = geoda.getCol(nat, "HR60");
console.log("hr60:", hr60);
```

{% endtab %}

{% tab title="Pure js" %}

```markup
<html>
<head>
<script src='bundle.js'></script>
<script>
jsgeoda.New().then((geoda)=> {
    console.log("Hello jsgeoda", geoda.version);
    
    const map_url = '/data/natregimes.geojson';
    
    // fetch the geojson and read it as ArrayBuffer
    fetch(map_url).then(res => res.arrayBuffer()).then((data) => {
    
        // load geojson in jsgeoda, an unique id (string) will be returned for further usage
        const nat = geoda.read_geojson(data);

        const num_obs = geoda.get_numobs(nat);
        console.log("number of observations:", num_obs);

        const col_names = geoda.get_colnames(nat);
        console.log("column names:", col_names);

        const hr60 = geoda.get_col(nat, "HR60");
        console.log("hr60:", hr60);
    });
});
</script>
</head>
<body></body>
</html>
```

{% endtab %}

{% tab title="React" %}

```javascript
import React, { Component } from "react";
import ReactDOM from "react-dom";
import jsgeoda from "jsgeoda";

class App extends Component {
  constructor() {
    super();
    this.state = {
      mapId: "",
      numObs: 0,
      data: []
    };
  }

  // load spatial data when mount this component
  loadSpatialData(geoda) {
    fetch(`https://webgeoda.github.io/data/natregimes.geojson`)
      .then((res) => res.arrayBuffer())
      .then((data) => {
        // load geojson in jsgeoda, an unique id (string) will be returned for further usage
        const nat = geoda.read_geojson(data);
        const num_obs = geoda.get_numobs(nat);
        const hr60 = geoda.get_col(nat, "HR60");
        this.setState({
          mapId: nat,
          numObs: num_obs,
          data: JSON.stringify(hr60)
        });
      });
  }

  componentDidMount() {
    // jsgeoda.New() function will create an instance from WASM
    // object that you can used later for spatial data analysis
    jsgeoda.New().then((geoda) => {
      this.loadSpatialData(geoda);
    });
  }

  render() {
    const rowStyle = {
      "text-align": "left",
      padding: "10px",
      fontFamily: "Arial",
      width: "100%"
    };

    return (
      <div className="App">
        <h1>JsGeoDa: Load Spatial Data</h1>
        <div style={rowStyle}>map id: {this.state.mapId}</div>
        <div style={rowStyle}>number of observations: {this.state.numObs}</div>
        <div style={rowStyle}>hr60: {this.state.data}</div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

```

{% endtab %}
{% endtabs %}

#### Try it yourself in the playground

{% embed url="<https://codesandbox.io/s/2-load-spatial-data-dgcux?file=/src/index.js>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xunli.gitbook.io/jsgeoda/user-guide/load-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
