Jed Rembold
April 9, 2025

GF_SERVER_ROOT_URL environment
variable and set it to
https://${{RAILWAY_PUBLIC_DOMAIN}}
'$variable_name'

Unlike the tablefunc extension,
PostGIS does not generally come with plain
Postgres by default
The PostGIS extension will bring in a lot of extra functions and
data types, so you might consider creating a new database to contain GIS
type data (maybe analysis_gis)
Adding the extension to the database is the same as with other extensions:
CREATE EXTENSION postgis;| Type | Format | Comments |
|---|---|---|
Point |
'POINT (-74.9 42.7)' |
No comma separating, and longitude comes first! |
LineString |
'LINESTRING (-74.9 42.7, -75.1 42.7)' |
Comma separates coordinate pairs |
Polygon |
'POLYGON((74 42, 75 42, 76 43, 74 42))' |
Double parentheses, initial point repeated to close |
MultiPoint |
'MULTIPOINT(75 42, 74 43)' |
Comma separates coordinate pairs |
MultiLineString |
'MULTILINESTRING((76 43, 77 43), (78 43, 77 43))' |
Parentheses group individual lines |
MultiPolygon |
'MULTIPOLYGON(((74 43, 75 44, 74 45, 74 43), (81 40, 81 39, 82 39, 81 40)))' |
Still double parentheses to start and end, with 3rd parentheses grouping polygons |
spatial_ref_sys table, so you can query to
find SRIDs if necessary (though it is a bit clunky)geography
geometry
Two main methods of creating geography or geometry types:
ST_GeomFromText(WKT, SRID) creates a
geometry object to hold the spatial object given by the WKT with the
optional given SRID
ST_GeogFromText(WKT, SRID) creates a
geography object to hold the spatial object given by the WKT with the
optional given SRID
If you look at the output of one of these data types, it is not human-readable
SELECT ST_GeomFromText('POINT(-75 42)', 4326);
>>> 0101000020E61000000000000000C052C00000000000004540
SELECT ST_GeogFromText('POINT(-75 42)');
>>> 0101000020E61000000000000000C052400000000000004540ST_MakePoint(long, lat, [z,m]) will
create a geometric point with optional 3rd or 4th dimensions as
wellST_MakeLine(point1, point2) will create a
line from the first point to the second. There is an array option as
well.ST_MakePolygon(geometry_linestring, [cutout_linestring])
will create a geometric polygon using the provided linestring with
optional cutoutsSince these all by default make
geometry type objects with no SRID, it can be
desirable to set or convert as needed
Can have an SRID attached to them:
ST_SetSRID(|||object|||, |||SRID|||) will
attach the given SRID metadata to the objectCan be converted:
|||object|||::geography will convert to
the new type (maintaining any attached SRIDA common pattern might then look something like:
SELECT ST_SetSRID(ST_MakePoint(|||long col|||,|||lat col|||), 4326);B-Trees are not well suited for indexing coordinate information
Instead, PostGIS recommends using the Generalized Search Tree (GiST) index type
CREATE INDEX |||index name|||
ON |||table name|||
USING GIST (|||column name|||);ST_DWithin(|||point₁|||, |||point₂|||, |||distance|||)
returns a True or False depending on whether the two points are within
the given distance from one another
ST_Distance(|||point₁|||, |||point₂|||)
computes the distance between the two points
shp2pgsql on all operating systems
shp2pgsql utilizes several flags to control
its behavior
-I – sets up a GIST index on the geometry
column-s – specifies a specific SRID-W – specifies a particular encoding if
needed (sometimes necessary for location names)shp2pgsql -I -s |||SRID||| -W |||encoding||| |||shapefile|||.SHP |||table_name|||By itself, shp2pgsql will just
generate SQL
It can be more useful to pass that SQL directly into your database
In Bash, this can be done directly with the trusty
| operator
All together then, the command would look like below (all on one line)
shp2pgsql -I -s |||SRID||| -W |||encoding||| |||shapefile|||.SHP |||table_name||| | pgsql -d |||database||| -U postgresSince the shapefile spatial information will be encoded directly to a geometry type, it can be tricky to know what exactly you are working with at times
You can call the ST_AsText() function
on any geometry (or geography) object to output its WKT
representation
SELECT ST_AsText(|||geom|||)
FROM |||table name|||
LIMIT 1;
In general, you would need to take your information to another program for visualization purposes
For a quick view though, this site will let you enter in a WKT which it will then display
Can be used in conjunction with
ST_AsText to grab results for quick
visualization
You can use ST_Collect to aggregate an
entire column of singular geometries into one Multi-geometry object for
each of representation
SELECT ST_Collect(ST_AsText(|||geom point|||))
FROM |||table name|||ST_Area(|||poly|||) will return the area
of the provided polygon. This will be in SRID specified units if
geometry or square meters if geographyST_Within(|||point|||, |||poly|||) will
return a True/False as to whether the given point lies within the
provided polygon
ST_Intersects(|||geom₁|||, |||geom₂|||)
will return a True/False if there exists an intersection between the two
geometriesST_Intersection(|||geom₁|||, |||geom₂|||)
will return a new geometry representing the intersection between the two
geometries