| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.Esqueleto.Postgis
Description
Haskell bindings for postgres postgis for a good explenation see https://postgis.net/
Make sure to use the correct SpatialType.
Earth spanning applications should use Geography,
local applications should use Geometry because it's more convenient.
if you can't use a function for example when you're using Geography.
there is the option to st_transform_geography.
Synopsis
- data Postgis (spatialType :: SpatialType) point
- = Point point
- | MultiPoint (NonEmpty point)
- | Line (LineString point)
- | Multiline (NonEmpty (LineString point))
- | Polygon (LinearRing point)
- | MultiPolygon (NonEmpty (LinearRing point))
- | Collection (NonEmpty (PostgisGeometry point))
- data SpatialType
- getPoints :: PostgisGeometry point -> NonEmpty point
- st_contains :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value Bool)
- st_intersects :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value Bool)
- st_union :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a))
- st_unions :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a))
- st_dwithin :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value Double) -> SqlExpr (Value Bool)
- st_distance :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value Double)
- point :: forall (spatialType :: SpatialType). Double -> Double -> Postgis spatialType PointXY
- point_v :: forall (spatialType :: SpatialType). HasPgType spatialType => Double -> Double -> SqlExpr (Value (Postgis spatialType PointXY))
- st_point :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXY))
- st_point_xyz :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXYZ))
- st_point_xyzm :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXYZM))
- st_transform_geography :: SRID 'Linear -> SqlExpr (Value (Postgis 'Geography a)) -> SqlExpr (Value (Postgis 'Geometry a))
- st_transform_geometry :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geography a))
- data SRID (unit :: SridUnit)
- wgs84 :: SRID 'Degree
- mercator :: SRID 'Linear
- britishNationalGrid :: SRID 'Linear
- data SridUnit
- makePolygon :: (Eq point, Show point) => point -> point -> point -> Seq point -> LinearRing point
- type PostgisGeometry = Postgis 'Geometry
- class HasPgType (spatialType :: SpatialType)
- data PointXY = PointXY {}
- data PointXYZ = PointXYZ {}
- data PointXYZM = PointXYZM {}
Documentation
data Postgis (spatialType :: SpatialType) point Source #
like GeospatialGeometry but not partial, eg no empty geometries.
Also can put an inveriant on dimensions if a function requires it.
for example st_intersects PostgisGeometry PointXY can't work with PostgisGeometry PointXYZ.
PointXY indicates a 2 dimension space, and PointXYZ a three dimension space.
Constructors
| Point point | |
| MultiPoint (NonEmpty point) | |
| Line (LineString point) | |
| Multiline (NonEmpty (LineString point)) | |
| Polygon (LinearRing point) | |
| MultiPolygon (NonEmpty (LinearRing point)) | |
| Collection (NonEmpty (PostgisGeometry point)) |
Instances
data SpatialType Source #
Guarantees we don't accidently mix curved space with flat space. Postgis will catch this too, this just put's it in the type system.
getPoints :: PostgisGeometry point -> NonEmpty point Source #
unwrap postgis geometry so you can for example return it from an API
functions
Arguments
| :: SqlExpr (Value (Postgis 'Geometry a)) | geom a |
| -> SqlExpr (Value (Postgis 'Geometry a)) | geom b |
| -> SqlExpr (Value Bool) |
Returns TRUE if geometry A contains geometry B. https://postgis.net/docs/ST_Contains.html
Arguments
| :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) | geomA or geogA |
| -> SqlExpr (Value (Postgis spatialType a)) | geomB or geogB |
| -> SqlExpr (Value Bool) |
Returns true if two geometries intersect. Geometries intersect if they have any point in common. https://postgis.net/docs/ST_Intersects.html
st_union :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a)) Source #
allows union of geometries, eg group a bunch together, https://postgis.net/docs/ST_Union.html for example:
mCombined <- selectOne $ do grid <- from $ tableGrid pure $ st_union $ grid ^. GridGeom select $ do unit <- from $ tableUnit forM_ mCombined $ combined -> where_ $ (unit ^. UnitGeom)st_intersects(val $ unValue combined) pure unit
st_unions :: SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a)) -> SqlExpr (Value (Postgis 'Geometry a)) Source #
Arguments
| :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) | geometry g1 |
| -> SqlExpr (Value (Postgis spatialType a)) | geometry g2 |
| -> SqlExpr (Value Double) | distance of srid |
| -> SqlExpr (Value Bool) |
Returns true if the geometries are within a given distance https://postgis.net/docs/ST_DWithin.html
st_distance :: forall (spatialType :: SpatialType) a. SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value (Postgis spatialType a)) -> SqlExpr (Value Double) Source #
calculate the distance between two points https://postgis.net/docs/ST_Distance.html
points
point :: forall (spatialType :: SpatialType). Double -> Double -> Postgis spatialType PointXY Source #
point_v :: forall (spatialType :: SpatialType). HasPgType spatialType => Double -> Double -> SqlExpr (Value (Postgis spatialType PointXY)) Source #
st_point :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXY)) Source #
st_point_xyz :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXYZ)) Source #
st_point_xyzm :: forall (spatialType :: SpatialType). SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value Double) -> SqlExpr (Value (Postgis spatialType PointXYZM)) Source #
transform
st_transform_geography Source #
Arguments
| :: SRID 'Linear | |
| -> SqlExpr (Value (Postgis 'Geography a)) | g1 (library handles the conversion) |
| -> SqlExpr (Value (Postgis 'Geometry a)) |
Project a geography onto a geometry.
allows using of functions such as st_union which only work in flat space (geometry).
https://postgis.net/docs/ST_Transform.html
st_transform_geometry Source #
Arguments
| :: SqlExpr (Value (Postgis 'Geometry a)) | g1 (library handles the conversion) |
| -> SqlExpr (Value (Postgis 'Geography a)) |
project a geometry as a geography, assumes wgs84.
https://postgis.net/docs/ST_Transform.html
srid
data SRID (unit :: SridUnit) Source #
SRID
you can find your local like this: https://blog.rustprooflabs.com/2020/11/postgis-find-local-srid
geography appears to use wgs84. So I hardcoded the use going from geom to geography as that.
you can use the num instance to put in whatever. however, if you miss a srid please submit a PR.
Instances
| Num (SRID unit) Source # | |
Defined in Database.Esqueleto.Postgis | |
| PersistField (SRID unit) Source # | |
Defined in Database.Esqueleto.Postgis Methods toPersistValue :: SRID unit -> PersistValue # fromPersistValue :: PersistValue -> Either Text (SRID unit) # | |
wgs84 :: SRID 'Degree Source #
default for geography type in postgis. Geodetic CRS: WGS 84 https://epsg.io/4326
mercator :: SRID 'Linear Source #
most maps are in this, it has some large distortions further away from equator https://epsg.io/3857
britishNationalGrid :: SRID 'Linear Source #
if you're in england this is pretty good. https://epsg.io/27700
Diferent SRID come in different units,
important for converting from geograhy to geometry.
other
makePolygon :: (Eq point, Show point) => point -> point -> point -> Seq point -> LinearRing point Source #
checks if the first point is the last, and if not so makes it so. this is required for inserting into the database
type PostgisGeometry = Postgis 'Geometry Source #
backwards compatibility, initial version only dealt in geometry
class HasPgType (spatialType :: SpatialType) Source #
technical typeclass to bind a spatial type to a string value. because we represent the constructors as a datakind, we need this to go back to a value.
Minimal complete definition
pgType
re-exports
(GeoPositionWithoutCRS is a catch all for indeterminate CRSs and for expression of positions
before a CRS has been determined
Instances
Instances
| Generic PointXYZ | |||||
Defined in Data.Geospatial.Internal.BasicTypes Associated Types
| |||||
| Show PointXYZ | |||||
| NFData PointXYZ | |||||
Defined in Data.Geospatial.Internal.BasicTypes | |||||
| Eq PointXYZ | |||||
| HasPgType spatialType => PersistField (Postgis spatialType PointXYZ) Source # | |||||
Defined in Database.Esqueleto.Postgis Methods toPersistValue :: Postgis spatialType PointXYZ -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Postgis spatialType PointXYZ) # | |||||
| HasPgType spatialType => PersistFieldSql (Postgis spatialType PointXYZ) Source # | |||||
| type Rep PointXYZ | |||||
Defined in Data.Geospatial.Internal.BasicTypes type Rep PointXYZ = D1 ('MetaData "PointXYZ" "Data.Geospatial.Internal.BasicTypes" "geojson-4.1.3-CvRMTkCmXKrLRGaX0qKQVX" 'False) (C1 ('MetaCons "PointXYZ" 'PrefixI 'True) (S1 ('MetaSel ('Just "_xyzX") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: (S1 ('MetaSel ('Just "_xyzY") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: S1 ('MetaSel ('Just "_xyzZ") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)))) | |||||
Instances
| Generic PointXYZM | |||||
Defined in Data.Geospatial.Internal.BasicTypes Associated Types
| |||||
| Show PointXYZM | |||||
| NFData PointXYZM | |||||
Defined in Data.Geospatial.Internal.BasicTypes | |||||
| Eq PointXYZM | |||||
| HasPgType spatialType => PersistField (Postgis spatialType PointXYZM) Source # | |||||
Defined in Database.Esqueleto.Postgis Methods toPersistValue :: Postgis spatialType PointXYZM -> PersistValue # fromPersistValue :: PersistValue -> Either Text (Postgis spatialType PointXYZM) # | |||||
| HasPgType spatialType => PersistFieldSql (Postgis spatialType PointXYZM) Source # | |||||
| type Rep PointXYZM | |||||
Defined in Data.Geospatial.Internal.BasicTypes type Rep PointXYZM = D1 ('MetaData "PointXYZM" "Data.Geospatial.Internal.BasicTypes" "geojson-4.1.3-CvRMTkCmXKrLRGaX0qKQVX" 'False) (C1 ('MetaCons "PointXYZM" 'PrefixI 'True) ((S1 ('MetaSel ('Just "_xyzmX") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: S1 ('MetaSel ('Just "_xyzmY") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)) :*: (S1 ('MetaSel ('Just "_xyzmZ") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double) :*: S1 ('MetaSel ('Just "_xyzmM") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Double)))) | |||||