0.2.0 • Published 10 months ago

geography-markup-language v0.2.0

Weekly downloads
-
License
CC0-1.0
Repository
github
Last release
10 months ago

geography-markup-language

Parse Geography Markup Language in Pure JavaScript

install

npm install geography-markup-language

usage

envelope

import { Envelope } from "geography-markup-language";

const xml = `
<gml:Envelope>
  <gml:lowerCorner>42.943 -71.032</gml:lowerCorner>
   <gml:upperCorner>43.039 -69.856</gml:upperCorner>
</gml:Envelope>
`;

Envelope(xml)
{
  srs: null,
  corners: [
    [42.943, -71.032], // lower corner
    [43.039, -69.856] // upper corner
  ]
}

// convert Envelope to GeoJSON
Envelope(xml, { format: "geojson" })
{
  type: "Feature",
  bbox: [-71.032, 42.943, -69.856, 43.039],
  properties: {},
  geometry: {
    type: "Polygon",
    coordinates: [
      [
        [-71.032, 43.039],
        [-71.032, 42.943],
        [-69.856, 42.943],
        [-69.856, 43.039],
        [-71.032, 43.039]
      ]
    ]
  }
}

LineString

import { LineString } from "geography-markup-language";

const xml = `
  <gml:LineString>
    <gml:posList>
      45.256 -110.45 46.46 -109.48 43.84 -109.86
    </gml:posList>
  </gml:LineString>
`;

LineString(xml)
{
  type: "LineString",
  coords: [
    [45.256, -110.45],
    [46.46, -109.48],
    [43.84, -109.86]
  ]
}

// convert line string into geojson
LineString(xml, { format: "geojson" });
 {
  type: "Feature",
  properties: {},
  geometry: {
    type: "LineString",
    coordinates: [
      [-110.45, 45.256],
      [-109.48, 46.46],
      [-109.86, 43.84]
    ]
  }
}

Polygon

import { Polygon } from "geography-markup-language";

const xml = `
<gml:Polygon>
    <gml:outerBoundaryIs>
        <gml:LinearRing>
            <gml:coordinates>0,0 100,0 100,100 0,100 0,0</gml:coordinates>
        </gml:LinearRing>
    </gml:outerBoundaryIs>
    <gml:innerBoundaryIs>
        <gml:LinearRing>
            <gml:coordinates>1,1 99,1 99,99 1,99 1,1</gml:coordinates>
        </gml:LinearRing>
    </gml:innerBoundaryIs>
</gml:Polygon>
`;

Polygon(xml, { format: "geojson" });
{
  type: "Feature",
  properties: {},
  geometry: {
    type: "Polygon",
    coordinates: [
      [
        [0, 0],
        [0, 100],
        [100, 100],
        [100, 0],
        [0, 0]
      ],
      [
        [1, 1],
        [1, 99],
        [99, 99],
        [99, 1],
        [1, 1]
      ]
    ]
  }
}

Point

import { Point } from "geography-markup-language";

// xml for Hawaii
const xml = `<gml:Point><gml:pos> 19.741755 -155.844437 </gml:pos></gml:Point>`;

Point(xml, { format: "geojson" })
{
  type: "Feature",
  properties: {},
  geometry: {
    type: "Point",
    coordinates: [-155.844437, 19.741755]
  }
}

Geometry

If you are not sure what type of geometry you are parsing, you can call Geometry. It will automatically determine which geometry is being parsed and internally call Envelope, LineString, Point, or Polygon accordingly.

import { Geometry } from "geography-markup-language";

// xml for Hawaii
const xml = `<gml:Point><gml:pos> 19.741755 -155.844437 </gml:pos></gml:Point>`;

Geometry(xml, { format: "geojson" })
{
  type: "Feature",
  properties: {},
  geometry: {
    type: "Point",
    coordinates: [-155.844437, 19.741755]
  }
}

references