Geofence an area

Get accurate data to provide context aware alerts or information to users.


1Set up you SafeGraph account

Create or log in to your SafeGraph account and create your API Key.

Now, lets include your API key in the request. Copy your API key and paste it below.

import requests
import json

headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined'
}

query = ""

req = requests.post(
    'https://api.safegraph.com/v2/graphql',
    json={ 'query': query },
    headers=headers
)

2Searching for a Location

The Places API allows you to specify the exact data you want. This means that you can ask for data from Core, Geometry, or Patterns for any location. In this example we are going to pull in some information about the location from the Core Patterns a add in Geometry data for the WKT data to map the location's shape.

We are going to make a request to search for the Starbucks located at 102 Pike St, Seattle, WA 98101.

Select a few of the fields below data you want to include in the response.

SafeGraph Core Fields (more fields here)

SafeGraph Core Fields

Latitude coordinate of the place of interest.

Longitude coordinate of the place of interest.

The geometric shape associated with this POI. Possible values are:1) POLYGON: POI has a polygon and geometry metadata available.2) POINT: POI does not have a polygon and does not have Geometry metadata.

import requests

headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined'
}
query = """
query {
    lookup(query: {
        location_name: "Starbucks"
        street_address: "102 Pike St"
        city: "Seattle"
        region:"WA"
        iso_country_code:"US"
    }) {
        placekey
        safegraph_core {
			latitude
			longitude
			geometry_type
        }
    }
}
"""

req = requests.post(
    'https://api.safegraph.com/v1/graphql',
    json={
        'query': query
    },
    headers=headers
)

3Request the WKT

The Places API allows you to specify the exact data you want. This means that you can ask for data from Core, Geometry, or Patterns for any location. In this example we are going to pull in some information about the location from the Core Patterns a add in Geometry data for the WKT data to map the location's shape.

We are going to make a request to search for the Starbucks located at 102 Pike St, Seattle, WA 98101.

Select a few of the fields below data you want to include in the response.

SafeGraph Geometry Fields (more fields here)

SafeGraph Geometry Fields

1) OWNED_POLYGON: Only one POI maps to this distinct polygon.2) SHARED_POLYGON: Atleast 2 POIs share this polygon

The shape of the place of interest, formatted as Well-Known Text (WKT). See polygon_wkt for more details.

Determines if this is a precise POI footprint polygon or synthetically created from an accurate centroid and category based radius.

Determines if this POI is completely enclosed indoors by its parent and is only accessible by entering the parent structure.

import requests

headers = {
    'Content-Type': 'application/json',
    'apikey': 'undefined'
}
query = """
query {
    lookup(query: {
        location_name: "Starbucks"
        street_address: "102 Pike St"
        city: "Seattle"
        region:"WA"
        iso_country_code:"US"
    }) {
        placekey
        safegraph_core {
			latitude
			longitude
			geometry_type
        }
        safegraph_geometry: {
			polygon_class
			polygon_wkt
			is_synthetic
			enclosed
        }
    }
}
"""

req = requests.post(
    'https://api.safegraph.com/v1/graphql',
    json={
        'query': query
    },
    headers=headers
)

4Getting the response

After you send the request, you will receive a response that will reflect the fields you included in the query.

{
  "data": {
    "lookup": {
      "placekey": "222-222@5x4-4b5-dvz",

      "safegraph_core": {
			"latitude": 47.609069,
			"longitude": -122.339897,
			"geometry_type": "POLYGON"
      },
      "safegraph_geometry": {
			"polygon_class": "OWNED_POLYGON",
			"polygon_wkt": "POLYGON ((-122.33987754232788 47.609097520763775, -122.33981690674591 47.60903745369943, -122.33995539203073 47.60898203990577, -122.34001475214961 47.60903964538951, -122.33987754232788 47.609097520763775))",
			"is_synthetic": false,
			"enclosed": true
      }
    }
  },
  "extensions": {
    "row_count": 1,
    "version_date": "1635494405__2021_10"
  }
}


4Mapping the WKT

Now we can select data.lookup.safegraph_geometry.polygon_wkt and set the bounds of the location.

r_json = req.json()

r_json['data']['lookup']['safegraph_geometry']['polygon_wkt']
WKT map example