Skip to content

Instantly share code, notes, and snippets.

@mike-marcacci
Created January 8, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-marcacci/a12e150949acf25006d1040e3dfe5bcb to your computer and use it in GitHub Desktop.
Save mike-marcacci/a12e150949acf25006d1040e3dfe5bcb to your computer and use it in GitHub Desktop.
Working around Google Cloud's Postgis ST_GeomFromGeoJSON failures
// @flow
import { mix } from 'mixwith';
import Model from '../../../Model';
import { type GeoJSONMultiPolygonCoordinates } from '../..';
import { NodeMixin, NodeInterface, type FieldDefinition } from '../../../core';
import { ObjectMixin, ObjectInterface } from '../../../delta';
import { AreaChange } from '../AreaChange/AreaChange';
import { Area } from '../Area/Area';
import wkx from 'wkx';
export class AreaObject extends mix(Model).with(NodeMixin, ObjectMixin) implements NodeInterface, ObjectInterface {
static table = 'content.area_object';
static get Change (): Class<AreaChange> {
return AreaChange;
};
static get Entity (): Class<Area> {
return Area;
};
+__typename: 'AreaObject' = 'AreaObject';
name: ?string;
description: ?string;
osmRelationId: ?number;
coordinates: GeoJSONMultiPolygonCoordinates;
constructor (data: *) {
super(data);
this.name = data.name;
this.description = data.description;
this.osmRelationId = data.osmRelationId;
this.coordinates = data.coordinates;
}
// get the field definitions
static getFieldDefinitions (): Map<string, FieldDefinition> {
const definitions = typeof super.getFieldDefinitions === 'function'
? super.getFieldDefinitions()
: new Map();
return definitions
.set('name', { column: 'name' })
.set('description', { column: 'description' })
.set('osmRelationId', { column: 'osm_relation_id' })
.set('coordinates', {
column: 'coordinates',
// https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/google-cloud-sql-discuss/u8XSP2kF__A/OIvgqE11BAAJ
// select: (prefix: string) => `ST_AsGeoJSON(${prefix ? `${prefix}.` : ''}coordinates)`,
// insert: (placeholder) => `ST_GeomFromGeoJSON(${placeholder})`,
// encode: (coordinates) => JSON.stringify({type: 'MultiPolygon', coordinates: coordinates}),
// decode: (geoJSON) => JSON.parse(geoJSON).coordinates,
select: (prefix: string) => `ST_AsBinary(${prefix}.coordinates)`,
insert: (placeholder) => `ST_GeometryFromText(${placeholder})`,
encode: (coordinates) => wkx.Geometry.parseGeoJSON({type: 'MultiPolygon', coordinates: coordinates}).toWkt(),
decode: (ewkt) => wkx.Geometry.parse(ewkt).toGeoJSON().coordinates,
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment