Created
January 8, 2018 19:00
Working around Google Cloud's Postgis ST_GeomFromGeoJSON failures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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