geojson-1.2.0: A thin GeoJSON Layer above the aeson library

MaintainerDom De Re
Safe HaskellNone




Refer to the GeoJSON Spec

A LinearRing is a List with at least 4 elements, where the first element is expected to be the same as the last.



data LinearRing a Source

a LinearRing has at least 3 (distinct) elements


Functor LinearRing 
Foldable LinearRing

This instance of Foldable will run through the entire ring, closing the loop by also passing the initial element in again at the end.

 (\xs -> (foldr (:) [] xs) == (fromLinearRing xs)) (xs :: LinearRing Int)
 (\xs -> (ringHead xs) == (foldr'' (\a -> const a) 0 xs)) (xs :: LinearRing Int)
Traversable LinearRing

When traversing this Structure, the Applicative context of the last element will be appended to the end to close the loop

Eq a => Eq (LinearRing a) 
Show a => Show (LinearRing a) 
ToJSON a => ToJSON (LinearRing a) 
(FromJSON a, Show a) => FromJSON (LinearRing a) 

data ListToLinearRingError a Source

When converting a List to a LinearRing there are some things that can go wrong

  • The list can be too short * The head may not be equal to the last element in the list (NB this is not currently checked due to performance concerns, and it also doesnt make much sense since its likely to contain doubles)



fromLinearRing :: LinearRing a -> [a]Source

This function converts it into a list and appends the given element to the end.

(xs -> safeLast (fromLinearRing xs) == Just (ringHead xs)) (xs :: LinearRing Int)

(xs -> length (fromLinearRing xs) >= 4) (xs :: LinearRing Int)

fromList :: (Validate v, Functor (v (NonEmpty (ListToLinearRingError a)))) => [a] -> v (NonEmpty (ListToLinearRingError a)) (LinearRing a)Source

creates a LinearRing out of a list of elements, if there arent enough elements (needs at least 4) elements

This version doesnt check equality of the head and tail in case you wish to use it for elements with no Eq instance defined.

Also its a list, finding the last element could be expensive with large lists. So just follow the spec and make sure the ring is closed.

Ideally the Spec would be modified to remove the redundant last element from the Polygons/LineRings. Its just going to waste bandwidth...

And be aware that the last element of the list will be dropped.

>>> fromList [] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccFailure (List too short: (length = 0) :| [])
>>> fromList [0] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccFailure (List too short: (length = 1) :| [])
>>> fromList [0, 1] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccFailure (List too short: (length = 2) :| [])
>>> fromList [0, 1, 2] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccFailure (List too short: (length = 3) :| [])
>>> fromList [0, 1, 2, 3] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccSuccess [0,1,2,0]
>>> fromList [0, 1, 2, 4, 0] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccSuccess [0,1,2,4,0]
>>> fromList [0, 1, 2, 4, 5, 0] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccSuccess [0,1,2,4,5,0]

Unfortunately it doesn't check that the last element is the same as the first at the moment...

>>> fromList [0, 1, 2, 4, 5, 6] :: AccValidation (NonEmpty (ListToLinearRingError Int)) (LinearRing Int)
AccSuccess [0,1,2,4,5,0]

fromListWithEqCheck :: (Eq a, Validate v, Applicative (v (NonEmpty (ListToLinearRingError a)))) => [a] -> v (NonEmpty (ListToLinearRingError a)) (LinearRing a)Source

The expensive version of fromList that checks whether the head and last elements are equal.



:: a

The first element

-> a

The second element

-> a

The third element

-> [a]

The rest of the optional elements (WITHOUT the first element repeated at the end)

-> LinearRing a 

Creates a LinearRing makeLinearRing x y z xs creates a LinearRing homomorphic to the list [x, y, z] ++ xs the list xs should NOT contain the first element repeated, i.e the loop does not need to be closed, makeLinearRing will close it off.

Repeating the first element is just redundant.

ringHead :: LinearRing a -> aSource

returns the element at the head of the ring

ringLength :: LinearRing a -> IntSource

returns the number of elements in the list, including the replicated element at the end of the list.

(xs -> ringLength xs == (length (fromLinearRing xs))) (xs :: LinearRing Int)