SQL queries
Create dynamic map layers in Felt using SQL queries.
Last updated
Was this helpful?
Create dynamic map layers in Felt using SQL queries.
Last updated
Was this helpful?
Felt’s custom SQL query feature lets you write and execute SQL queries directly against your database, enabling you to filter and manipulate your spatial data before visualizing it on your map. It gives you full control over how your data is displayed, ensuring your maps are tailored to meet your specific analysis needs. Whether you're refining data or creating complex visualizations, Felt makes it easy to seamlessly integrate your custom queries with your mapping workflow.
This feature is only available to customers on the Enterprise plan. To upgrade, contact sales.
First, connect the data source of your choice. Not sure how? No worries—check out the detailed instructions here. Once you're connected, you’re good to go and can start writing your custom SQL queries.
Go to your Library and find your data source in the left column under the Sources heading.
Select the source you want to use for your SQL query.
After selecting the source, click the SQL
icon in the top-right corner of the Library window to open the query editor.
The SQL query window will open, allowing you to write SQL statements in the code editor. The editor also provides suggestions for table and column names based on your database schema.
Click Run Preview
to execute your query. Felt will display a preview of the resulting data in the Preview window.
Click Create Layer
in the bottom-right corner of the Preview window to add your data to the map.
A new layer will be added to your map and you can start browsing your data and styling your map.
If your query isn’t valid, the Run Preview
button will display a !
to indicate an error.
Hover over the Run Preview
button to see a popup with helpful hints about potential issues in your query.
Fixing the syntax error in your query will remove the !
.
If your query doesn’t generate a preview, an error message will appear in the Preview window with details to help you debug the issue. Adjust your query and try again.
To view the query used to generate a layer, click the Data
heading in the layer window. Then, click the Source
name to open a SQL query window showing the query.
To create another layer using a similar query, click the Source
name to open a SQL query window, then select Copy to new query
.
If you want to create a layer without generating a preview, click the ˅
beside the Run Preview
button and select Create layer without previewing
.
Different SQL platforms offer varying levels of support for spatial operations, with some providing extensive libraries of geospatial functions and others offering more basic capabilities. While some focus on advanced geographic and geometric queries, others prioritize scalability and integration with broader data processing workflows. The specific capabilities and limitations depend on the platform, so refer to the relevant documentation linked below for more details.
Postgres (PostGIS)
The SQL query feature supports only SELECT
statements, ensuring read-only access to your database.
ST_Intersects
Tests whether two geometries share any space in common. Returns true if geometries intersect, false otherwise. Used for identifying overlapping areas.
SELECT a.*, b.* FROM service_areas a JOIN market_zones b ON ST_Intersects(a.geometry, b.geometry);
ST_Within
Tests whether one geometry is completely inside another. Returns true if the first geometry is completely within the second geometry.
SELECT a.name, b.name FROM points a JOIN polygons b ON ST_Within(a.geometry, b.geometry);
ST_Buffer
Creates a new geometry that represents all points within a given distance from the input geometry. Useful for creating service areas or zones of influence.
SELECT location_id, ST_Buffer(geometry, 5000) as service_area FROM distribution_centers;
ST_Centroid
Computes the geometric center (centroid) of a geometry. Returns a point that represents the average position of all points in the geometry.
SELECT region_name, ST_Centroid(geometry) as center_point FROM service_regions;
ST_Distance
Calculates the shortest distance between two geometries. Returns the minimum distance between any two points in the two geometries.
SELECT a.id, b.id, ST_Distance(a.geometry, b.geometry) as distance FROM points a CROSS JOIN points b WHERE a.id != b.id;
ST_Area
Calculates the area of a polygon geometry. Returns the area in the units of the spatial reference system.
SELECT territory_id, ST_Area(geometry::geography) as area_sqm FROM market_territories;
ST_Length
Calculates the length of a linear geometry (linestring). Returns the length in the units of the spatial reference system.
SELECT route_id, ST_Length(geometry::geography) as length_meters FROM delivery_routes;
ST_Union
Combines multiple geometries into a single geometry. Dissolves overlapping boundaries and creates a single continuous geometry.
SELECT region_id, ST_Union(geometry) as combined_geometry FROM parcels GROUP BY region_id;
ST_Intersection
Returns the geometry representing the shared portions of two geometries. Creates a new geometry containing only the overlapping areas.
SELECT ST_Intersection(a.geometry, b.geometry) as overlap FROM zone_a a CROSS JOIN zone_b b;
ST_Transform
Transforms a geometry from one coordinate system to another. Used to convert between different spatial reference systems (SRS).
SELECT id, ST_Transform(geometry, 4326) as wgs84_geom FROM locations;
ST_Envelope
Computes the minimum bounding rectangle that contains a geometry. Returns a rectangular polygon that completely encloses the input geometry.
SELECT region_id, ST_Envelope(ST_Collect(geometry)) as bbox FROM market_regions GROUP BY region_id;
ST_ConvexHull
Computes the smallest convex polygon that contains all points in a geometry. Creates a shape that represents the outer boundary of a set of points.
SELECT cluster_id, ST_ConvexHull(ST_Collect(geometry)) as boundary FROM points GROUP BY cluster_id;
ST_Contains
Tests whether one geometry completely contains another. Returns true if the second geometry is completely contained within the first geometry.
SELECT points.* FROM customer_points points JOIN service_areas areas ON ST_Contains(areas.geometry, points.geometry);