am2222 / strapi-plugin-postgis

Add native postgis support to strapi.
https://am2222.github.io/strapi-plugin-postgis/
41 stars 15 forks source link
gis postgis strapi-plugin strapipluginweek
[![DocSearch](https://github.com/am2222/strapi-plugin-postgis/raw/main/images/github-logo.png?raw=true)](https://am2222.github.io/strapi-plugin-postgis/) Add native postgis support to strapi. [![npm Package](https://github.com/am2222/strapi-plugin-postgis/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/am2222/strapi-plugin-postgis/actions/workflows/npm-publish.yml) [![Docs](https://github.com/am2222/strapi-plugin-postgis/actions/workflows/docs-publish-github.yml/badge.svg)](https://github.com/am2222/strapi-plugin-postgis/actions/workflows/docs-publish-github.yml)

Documentation Video

Screenshot


Under Development

How does it work?

Since Strapi does not support native database formats I convert requests before they being sent to the querybuilder and convert all the geometry objects to the geojson.

Requirements

Strapi Version 4.5.0 and up.

Installation

Setup your strapi app as usual

npx create-strapi-app@latest my-project --quickstart

Install pg and strapi-plugin-postgis

npm install pg --save
npm i strapi-plugin-postgis

Make sure to config your strapi to use postgrs database as backend, Use this link in case you need any help with this step (https://strapi.io/blog/postgre-sql-and-strapi-setup)

Make sure to install postgis on your database server.

Modify your middlewares as following to let strapi load osm tiles. Add '*.tile.openstreetmap.org' to the img-src as follows

// ./config/middlewares.js
module.exports = [
  'strapi::errors',
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'img-src': ["'self'", 'data:', 'blob:', '*.tile.openstreetmap.org'],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

Run strapi and you should see the following line


[2022-06-03 10:47:25.194] info: Welcome to Strapi Postgis 🚀 + 🐘 + 🗺️ | 3.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1

Now in your content-types api folder modify schema.json and add a new column with the following format

{
  "kind": "collectionType",
  .
  .
  .
  "attributes": {
  .
  .
  .
    "geom": { //--> your column name. you can change to anything
      "columnType": {
        "type": "specificType",
        "args": [
          "geometry(POINT,4326)" //-> change this line according to the Supported Data Types section
        ]
      },
      "type": "customField", //->don't change this
      "customField": "plugin::postgis.map" //->don't change this
    }
  }
}

Supported Data Types

POINT

Screenshot

"geom": {
      "columnType": {
        "type": "specificType",
        "args": [
          "geometry(POINT,4326)"
        ]
      },
      "type": "customField",
      "customField": "plugin::postgis.map"
    }

LINESTRING

Screenshot

    "g_line": {
      "columnType": {
        "type": "specificType",
        "args": [
          "geometry(LINESTRING,4326)"
        ]
      },
      "type": "customField",
      "customField": "plugin::postgis.map"
    }

POLYGON

Screenshot

    "g_polygon": {
      "columnType": {
        "type": "specificType",
        "args": [
          "geometry(POLYGON,4326)"
        ]
      },
      "type": "customField",
      "customField": "plugin::postgis.map"
    }

TODO

Thanks to