🗺️
Mapster WP Maps
HomeProExamplesContact
  • Getting Started
    • About
    • Installation
    • Key Concepts
    • Importing/Exporting
    • Guides
      • Your First Map
      • Creating a Crowdsourced Map
      • Set Up Recurring Import
      • Adding a Custom Style
      • Building a Custom Popup
  • Creating Shapes
  • Displaying Maps
  • Maps
    • Map Tiles
    • Initial View
      • Fit to Features
      • Manual Values
      • User Location
    • Map Size
    • Points, Lines, Polygons
    • Clustering
    • Interactivity
    • Controls
      • Basic Controls
      • Geocoding & Geolocation
      • Togglers
      • Other Controls
      • Control Render Order
    • Filters
      • Category Filter
      • Custom Search Filter
      • Filter Dropdown
    • Lists
      • Store Locator
    • Specialty Maps
      • Map Comparer
      • Heatmaps
      • Elevation Profile
      • User Submission
      • Listing Page Map
    • Preloader
    • Developer
      • Javascript
      • Embedding Maps
      • Faster Loading (Cache)
      • Caching
      • Progressive Loading
  • Locations
    • Markers
    • Circles
    • Labels
    • Icons
    • 3D Models
  • Lines
    • Lines
  • Multi Lines
  • Polygons
    • Flat Polygons
    • 3D Polygons
    • Image Polygons
  • Multi Polygons
  • Popups
    • Popup Templates
      • Introduction
      • Layout, Colors, Text, CSS
      • HTML
      • Options
    • Popup Content
  • Pro Sections
    • User Submission
      • Overview
      • Submission Interface
      • Adding Custom Fields
      • Submission Admin
  • Mass Edit
  • Settings
    • General
    • Account Management
  • Advanced
  • Import
  • Tileset Management
  • Developer
    • Mapster Map ACF Field
    • Mapster Map Gravity Forms Field
    • Custom Scripts
    • Hooks
    • Spatial DB & API
    • Internationalization
Powered by GitBook
On this page
  • Creating the Spatial Database
  • Querying the Spatial Database
  • Creating an API
  • Example Queries

Was this helpful?

  1. Developer

Spatial DB & API

PreviousHooksNextInternationalization

Last updated 20 hours ago

Was this helpful?

In Pro, you can really superpower your Wordpress site and turn it into a proper spatial backend for other apps, external users, or other types of spatial queries. The database Wordpress uses, MySQL, does contain quite a bit of spatial functionality -- not as much as PostGIS and PostgreSQL, but enough for most usage for most spatial projects.

Creating the Spatial Database

To create a spatial table in your Mapster WP Maps installation, make sure you have Pro installed, and go to Maps > Settings. Click on the Advanced Options tab and scroll down to the Spatial API option. Once this is enabled, on the next page load your spatial database will be created and populated with all the features that currently exist in your installation.

The mapster_maps_geometry table contains just three columns: an identifying ID, a post_id corresponding to the post, and a coordinates column containing the geometry for that post. The geometry column will be used for spatial queries and space is minimized by not storing any additional information.

As long as the Spatial API option is enabled, the database will continue to be updated as you create new geometries and update existing geometries.

Querying the Spatial Database

Creating an API

This is a feature that can help you connect your Wordpress site to other applications, external users, or other management platforms. Mapster has built in a query endpoint that can receive a variety of queries and perform these queries on your spatial database.

Responses will return as a full GeoJSON, with found posts as GeoJSON features.

The endpoint for your queryable endpoint will be

yourwordpressinstall.com/wp-json/mapster-wp-maps/query

This is a POST endpoint. Available query parameters include:

Parameter
Input Type

category

integer

ID for a Mapster WP Maps category

query_type

string

pip for Point-in-Polygon, polygon-overlap for Polygon Overlap

query_feature

geoJSON feature object

Point or Polygon depending on the query

bounds_only

boolean

Output the whole feature (false) or only the feature bounds (true)

custom_fields

array of strings

An array of the names of Advanced Custom Fields you want to include as feature properties

Example Queries

Point in Polygon

The following query performs a point-in-polygon query on category 8, returning the full feature geoJSONs along with feature properties school_name and activities.

Query

fetch('http://localhost:8888/wp-json/mapster-wp-maps/query', {
    method : "POST",
    body : JSON.stringify({
        "category" : 8, // A Mapster WP Maps Category
        "query_type" : "pip",
        "query_feature" : {
            "type" : "Feature",
            "properties" : {},
            "geometry" : {
                "type" : "Point",
                "coordinates" : [79.804687, 60.930432202923335]
            }
        },
        "bounds_only" : false,
        "custom_fields" : ["school_name", "activities"]
    })
}).then(resp => resp.json()).then(response => {
    console.log(response)
});

Response

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "post_id": "1687",
                "title": "Russia",
                "permalink": "http://localhost:8888/mapster-wp-polygon/russia/",
                "excerpt": "",
                "school_name": null,
                "activities": null
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        ...
                    ]
                ]
            }
        }
    ]
}

Polygon Overlap

This will check for intersecting polygons in category 8, and return the full feature geoJSON.

fetch('http://localhost:8888/wp-json/mapster-wp-maps/query', {
    method : "POST",
    body : JSON.stringify({
      "category" : 8,
      "query_type" : "polygon-overlap",
      "query_feature" : {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [ 
              ...
            ]
          ]
        }
      }
  })
}).then(resp => resp.json()).then(response => {
    console.log(response)
});

SQL has a number of spatial queries you can do if you're into writing your own SQL queries. You can find a list of them at the . Your spatial data is stored as geometries in the coordinates column.

Microsoft documentation here