Create a layer

After creating a location using the /workplace/v1/locations endpoint, you can create a layer (representing a floor) using the @gospace-ai/api SDK. Layers can be created via the /spatial/v1/layers endpoint with geometry data (WKT, GeoJSON, or IMDF) or by uploading a floorplan file in DXF, DWG, PNG, JPEG, or PDF format to auto-populate spaces and rooms.

Option 1 - Spatial API

Create a Script

Create a JavaScript file to create a layer with WKT geometry:

// create_layer.js
const { GospaceClient } = require('@gospace-ai/api');
require('dotenv').config();

async function createLayer() {
  try {
    const client = new GospaceClient({ api_key: process.env.GOSPACE_API_KEY || '' });
    const layer = await client.spatial.layersCreate({
      location_id: 'loc_12345',
      name: 'Floor 1',
      geometry: {
        type: 'WKT',
        data: 'MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)))'
      }
    });
    console.log('Layer created:', layer.id);
  } catch (error) {
    console.error('Error creating layer:', error);
  }
}

createLayer();

Run the Script

Ensure your .env file contains a valid GOSPACE_API_KEY, then run:

node create_layer.js

You should see the created layer’s ID printed, such as:json

Layer created: lay_67890

Option 2 - Floorplan Upload

Install Dependencies

Install form-data for file uploads:

npm install form-data

Create a Script

Create a JavaScript file to upload a floorplan (e.g., DXF, DWG, PNG, JPEG, or PDF):

// upload_floorplan.js
const { GospaceClient } = require('@gospace-ai/api');
const FormData = require('form-data');
const fs = require('fs');
require('dotenv').config();

async function uploadFloorplan() {
  try {
    const client = new GospaceClient({ api_key: process.env.GOSPACE_API_KEY || '' });
    const form = new FormData();
    form.append('location_id', 'loc_12345');
    form.append('name', 'Floor 1');
    form.append('floorplan', fs.createReadStream('path/to/floorplan.dxf'));

    const layer = await client.spatial.layersUpload(form);
    console.log('Layer created with floorplan:', layer.id);
  } catch (error) {
    console.error('Error uploading floorplan:', error);
  }
}

uploadFloorplan();

Run the Script

Replace path/to/floorplan.dxf with the actual file path (e.g., .dxf, .dwg, .png, .jpeg, or .pdf), ensure your .env file has a valid GOSPACE_API_KEY, then run:

node upload_floorplan.js

You should see the created layer’s ID printed, such as:

Layer created with floorplan: lay_67890

Note: Replace 'loc_12345' with a valid location_id. Supported floorplan formats are DXF, DWG, PNG, JPEG, and PDF. The @gospace-ai/api SDK supports TypeScript for type-safe development. To use TypeScript, rename the file to .ts (e.g., create_layer.ts), install ts-node (npm install ts-node --save-dev), and run with npx ts-node create_layer.ts. Floorplan uploads auto-populate spaces and rooms; otherwise, create them manually with spacesCreate or roomsCreate. See developer.gospace.com for details.

Last updated

Was this helpful?