Skip to content

Commit

Permalink
Add Polygon/LineString support
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Nov 21, 2023
1 parent 6ba7813 commit d9ba9cd
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 119 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

## Version History

### v4.4.0

- :rocket: Support `Polygon` & `LineString` `to_geojson()`
- :bug: Fix previously unused Packed colour => opacity

### v4.3.0

- :bug: Fix Opacity default to a non-zero value
Expand Down
6 changes: 5 additions & 1 deletion lib/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class Color {
this.r = (color >> 16) & 255;
this.g = (color >> 8) & 255;
this.b = (color >> 0) & 255;
this.a = ((color >> 24) & 255) / 255;
this.a = ((color >> 24) & 255);
} else if (Array.isArray(color)) {
this.a = color[0];
this.r = color[1];
Expand All @@ -35,6 +35,10 @@ export default class Color {
}
}

as_hex(): string {
return _color.rgb(this.r, this.g, this.b).hex();
}

as_32bit(): number {
return (this.a << 24) | (this.r << 16) | (this.g << 8) | this.b;
}
Expand Down
58 changes: 49 additions & 9 deletions lib/cot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import xmljs from 'xml-js';
import { Feature } from 'geojson';
import { Feature, Geometry } from 'geojson';

Check failure on line 2 in lib/cot.ts

View workflow job for this annotation

GitHub Actions / test

'Geometry' is defined but never used
import { AllGeoJSON } from "@turf/helpers";
import Util from './util.js';
import Color from './color.js';
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface Detail {
strokeStyle?: GenericAttributes,
labels_on?: GenericAttributes,
fillColor?: GenericAttributes,
link?: object[],
link?: GenericAttributes[],
usericon?: GenericAttributes,
track?: Track,
TakControl?: {
Expand Down Expand Up @@ -210,7 +210,7 @@ export default class CoT {
if (!raw.event.detail.contact) raw.event.detail.contact = {};
if (!raw.event.detail.contact._attributes) raw.event.detail.contact._attributes = {};

const geojson: Feature = {
const feat: Feature = {
id: raw.event._attributes.uid,
type: 'Feature',
properties: {
Expand All @@ -231,22 +231,62 @@ export default class CoT {
}
};

if (!geojson.properties) geojson.properties = {};
if (!feat.properties) feat.properties = {};

if (raw.event.detail.remarks && raw.event.detail.remarks._text) {
geojson.properties.remarks = raw.event.detail.remarks._text;
feat.properties.remarks = raw.event.detail.remarks._text;
}

if (raw.event.detail.track && raw.event.detail.track._attributes) {
if (raw.event.detail.track._attributes.course) geojson.properties.course = Number(raw.event.detail.track._attributes.course);
if (raw.event.detail.track._attributes.course) geojson.properties.speed = Number(raw.event.detail.track._attributes.speed);
if (raw.event.detail.track._attributes.course) feat.properties.course = Number(raw.event.detail.track._attributes.course);
if (raw.event.detail.track._attributes.course) feat.properties.speed = Number(raw.event.detail.track._attributes.speed);
}

if (raw.event.detail.usericon && raw.event.detail.usericon._attributes && raw.event.detail.usericon._attributes.iconsetpath) {
geojson.properties.icon = raw.event.detail.usericon._attributes.iconsetpath;
feat.properties.icon = raw.event.detail.usericon._attributes.iconsetpath;
}

return geojson;

if (this.raw.event._attributes.type === 'u-d-f' && Array.isArray(this.raw.event.detail.link)) {
const coordinates = [];

for (const l of this.raw.event.detail.link) {
coordinates.push(l._attributes.point.split(',').map((p: string) => { return Number(p.trim()) }).splice(0, 2).reverse());
}

if (this.raw.event.detail.strokeColor && this.raw.event.detail.strokeColor._attributes && this.raw.event.detail.strokeColor._attributes.value) {
const stroke = new Color(Number(this.raw.event.detail.strokeColor._attributes.value));
feat.properties.stroke = stroke.as_hex();
feat.properties['stroke-opacity'] = stroke.as_opacity();
}

if (this.raw.event.detail.strokeWeight && this.raw.event.detail.strokeWeight._attributes && this.raw.event.detail.strokeWeight._attributes.value) {
feat.properties['stroke-width'] = Number(this.raw.event.detail.strokeWeight._attributes.value);
}

if (this.raw.event.detail.strokeStyle && this.raw.event.detail.strokeStyle._attributes && this.raw.event.detail.strokeStyle._attributes.value) {
feat.properties['stroke-style'] = this.raw.event.detail.strokeStyle._attributes.value;
}

if (coordinates[0][0] === coordinates[coordinates.length -1][0] && coordinates[0][1] === coordinates[coordinates.length -1][1]) {
feat.geometry = {
type: 'Polygon',
coordinates: [coordinates]
}

if (this.raw.event.detail.fillColor && this.raw.event.detail.fillColor._attributes && this.raw.event.detail.fillColor._attributes.value) {
const fill = new Color(Number(this.raw.event.detail.fillColor._attributes.value));
feat.properties['fill-opacity'] = fill.as_opacity();
feat.properties['fill'] = fill.as_hex();
}
} else {
feat.geometry = {
type: 'LineString',
coordinates
}
}
}
return feat;
}

to_xml(): string {
Expand Down
Loading

0 comments on commit d9ba9cd

Please sign in to comment.