Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Last active March 19, 2019 11:21
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 Rycochet/55d8720636392ca1c6795139fa2c569e to your computer and use it in GitHub Desktop.
Save Rycochet/55d8720636392ca1c6795139fa2c569e to your computer and use it in GitHub Desktop.
const PostGIS = {
// FUNCTIONS
gisContains: {
description: "Returns TRUE if and only if no points of supplied lie in the exterior of field, and at least one point of the interior of supplied lies in the interior of field.",
fname: "ST_Contains",
},
gisContainsProperly: {
description: "Returns TRUE if supplied intersects the interior of field but not the boundary (or exterior). Field does not contain properly itself, but does contain itself.",
fname: "ST_ContainsProperly",
},
gisCoveredBy: {
description: "Returns TRUE if no point in field Geometry/Geography is outside supplied Geometry/Geography.",
fname: "ST_CoveredBy",
},
gisCovers: {
description: "Returns TRUE if no point in supplied Geometry is outside field Geometry.",
fname: "ST_Covers",
},
gisCrosses: {
description: "Returns TRUE if the supplied geometries have some, but not all, interior points in common.",
fname: "ST_Crosses",
},
gisDisjoint: {
description: "Returns TRUE if the Geometries do not 'spatially intersect' - if they do not share any space together.",
fname: "ST_Disjoint",
},
gisEquals: {
description: "Returns TRUE if the given geometries represent the same geometry. Directionality is ignored.",
fname: "ST_Equals",
},
gisIntersects: {
description: "Returns TRUE if the Geometries/Geography 'spatially intersect in 2D' - (share any portion of space).",
fname: "ST_Intersects",
},
gisIntersects3D: {
description: "Returns TRUE if the Geometries 'spatially intersect' in 3d - only for points, linestrings, polygons, polyhedral surface (area). With SFCGAL backend enabled also supports TINS.",
fname: "ST_3DIntersects",
},
gisOrderingEquals: {
description: "Returns TRUE if the given geometries represent the same geometry and points are in the same directional order.",
fname: "ST_OrderingEquals",
},
gisOverlaps: {
description: "Returns TRUE if the Geometries share space, are of the same dimension, but are not completely contained by each other.",
fname: "ST_Overlaps",
},
gisTouches: {
description: "Returns TRUE if the geometries have at least one point in common, but their interiors do not intersect.",
fname: "ST_Touches",
},
gisWithin: {
description: "Returns TRUE if the geometry field is completely inside geometry supplied",
fname: "ST_Within",
},
// OPERATORS
bboxAbove: {
description: "Returns TRUE if field's bounding box is strictly above supplied's.",
operator: "|>>",
},
bboxBelow: {
description: "Returns TRUE if field's bounding box is strictly below supplied's.",
operator: "<<|",
},
bboxContains: {
description: "Returns TRUE if field's bounding box contains supplied's.",
operator: "~",
},
bboxEquals: {
description: "Returns TRUE if field's bounding box is the same as supplied's.",
operator: "~=",
},
bboxIntersects2D: {
description: "Returns TRUE if fields's 2D bounding box intersects supplied's 2D bounding box.",
operator: "&&",
},
bboxIntersectsND: {
description: "Returns TRUE if the coordinates and coordinate order geometry/geography field are the same as the coordinates and coordinate order of geometry/geography supplied.",
operator: "=",
},
bboxLeftOf: {
description: "Returns TRUE if field's bounding box is strictly to the left of supplied's.",
operator: "<<",
},
bboxOverlapsOrAbove: {
description: "Returns TRUE if field's bounding box overlaps or is above supplied's.",
operator: "|&>",
},
bboxOverlapsOrBelow: {
description: "Returns TRUE if field's bounding box overlaps or is below supplied's.",
operator: "&<|",
},
bboxOverlapsOrLeftOf: {
description: "Returns TRUE if field's bounding box overlaps or is to the left of supplied's.",
operator: "&<",
},
bboxOverlapsOrRightOf: {
description: "Returns TRUE if field's bounding box overlaps or is to the right of supplied's.",
operator: "&>",
},
bboxRightOf: {
description: "Returns TRUE if field's bounding box is strictly to the right of supplied's.",
operator: ">>",
},
gisExactlyEquals: {
description: "Returns TRUE if the coordinates and coordinate order geometry/geography field are the same as the coordinates and coordinate order of geometry/geography supplied.",
operator: "=",
},
};
module.exports = function PgConnectionArgFilterPostgisOperatorsPlugin(builder) {
builder.hook("build", (build, input) => {
const { addConnectionFilterOperator, pgSql: sql } = build;
if (addConnectionFilterOperator) {
for (const name in PostGIS) {
const { description, fname = "", operator = "," } = PostGIS[name];
const sqlFName = sql.raw(fname);
const sqlOperator = sql.raw(operator);
addConnectionFilterOperator(
name,
description,
(fieldInputType) => fieldInputType,
(identifier, val) => {
console.log("SQL", sql, sql.query);
console.log("GEO", sql.escapeSqlIdentifier(val), val);
console.log("IDENT", sql.escapeSqlIdentifier(identifier), identifier);
return sql.query`CASE WHEN ST_SRID(${identifier}) <> ST_SRID(${val})
THEN ${sqlFName}(${identifier} ${sqlOperator} ST_Transform(${val}, ST_SRID(${identifier})))
ELSE ${sqlFName}(${identifier} ${sqlOperator} ${val})
END`
}, {
allowedFieldTypes: ["GeographyPoint"],
},
);
}
} else {
console.warn("PostGIS filters must be added after 'postgraphile-plugin-connection-filter'");
}
return input;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment