Created
February 18, 2018 10:54
-
-
Save naoto-ogawa/1d72a4366772397663fcfcd97ef8834a to your computer and use it in GitHub Desktop.
A point geometry sample on MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE ConstraintKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module Example.Example15 where | |
import Control.Exception.Safe (SomeException, catch) | |
-- my library | |
import DataBase.MySQLX.NodeSession | |
import DataBase.MySQLX.ResultSet | |
import DataBase.MySQLX.Statement | |
import qualified Data.ByteString.Lazy as BL | |
import qualified Data.Sequence as Seq | |
import Data.Maybe | |
import Data.Geometry.Geos.Geometry | |
import Data.Geometry.Geos.Serialize | |
import Data.Geometry.Geos.Types | |
{- | |
create table points_tbl (`id` int unsigned not null auto_increment, `location` Point not null, primary key(`id`)); | |
insert into points_tbl (location) values (point(1,1)); | |
mysql-sql> select st_astext(location) from points_tbl; | |
+---------------------+ | |
| st_astext(location) | | |
+---------------------+ | |
| POINT(1 1) | | |
+---------------------+ | |
1 row in set (0.01 sec) | |
-} | |
data Geo = Geo { | |
p :: Geometry Point | |
} deriving Show | |
geo:: RowFrom Geo | |
geo= Geo <$> colVal | |
getColGeoPoint :: Row -> Int -> Geometry Point | |
getColGeoPoint row idx = getColGeoPoint' $ Seq.index row idx | |
getColGeoPoint' :: BL.ByteString -> Geometry Point | |
getColGeoPoint' x = ensurePoint $ fromJust $ readWkb (BL.toStrict $ BL.drop 4 $ getColByteString' x) | |
getColGeoPoint'' :: BL.ByteString -> Geometry Point | |
getColGeoPoint'' x = ensurePoint $ fromJust $ readWkb (BL.toStrict $ getColByteString' x) | |
instance ColumnValuable (Geometry Point) where toColVal' = getColGeoPoint' | |
example15 :: IO () | |
example15 = do | |
nodeSess <- openNodeSession $ defaultNodeSesssionInfo {database = "x_protocol_test", user = "root", password="root", port=8000} | |
catch ( do | |
result <- executeRawSql "select location from points_tbl" nodeSess | |
print $ rowFrom geo $ head result | |
) (\(e::SomeException) -> print e) | |
closeNodeSession nodeSess | |
{- | |
*Example.Example15> example15 | |
Geo {p = PointGeometry (Point (Coordinate2 1.0 1.0)) Nothing} | |
*Example.Example15> | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment