123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294(* Copyright (c) 2013 Thomas Gazagnaire <thomas@gazagnaire.org>
Copyright (c) 2021-2022 Patrick Ferris <patrick@sirref.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
The defunctionalised value construction is borrowed from Ezjsone.
*)(* A GeoJson document consists of a single JSON document that is either a feature collection
(an array of features), a single feature (an array of geometry objects) or a single geometry
objects (which could contain multiple geometry objects thanks to the collection type).
Most commmonly, the large size of a GeoJson document is because it is a feature collection
containing many features, although it's probably not infeasible that there are huge documents
containing a single feature with lots of geometry objects. *)moduleErr=structtypelocation=(int*int)*(int*int)typet=[`Erroroflocation*Jsone.error|`EOI|`Unexpectedofstring]letppppf=function|`Error(((l1,l2),(l3,l4)),e)->Format.fprintfppf"Error %a (%i:%i - %i:%i)"Jsone.pp_errorel1l2l3l4|`EOI->Format.fprintfppf"Unexpected end of input"|`Unexpecteds->Format.fprintfppf"Unexpected %s"sendexceptionAbortofErr.tmoduleG=structmoduleEzjsone_parser=structtypet=Ezjsone.valueletcatch_errfv=tryOk(fv)withEzjsone.Parse_error(_,s)->Error(`Msgs)letfind=Ezjsone.find_optletto_stringt=catch_errEzjsone.get_stringtletstring=Ezjsone.stringletto_floatt=catch_errEzjsone.get_floattletfloat=Ezjsone.floatletto_intt=catch_errEzjsone.get_inttletint=Ezjsone.intletto_listft=catch_err(Ezjsone.get_listf)tletlistft=Ezjsone.listftletto_arrayft=Result.mapArray.of_list@@to_listftletarrayft=listf(Array.to_listt)letto_objt=catch_errEzjsone.get_dicttletobj=Ezjsone.dictletnull=`Nullletis_null=function`Null->true|_->falseendincludeGeojson.Make(Ezjsone_parser)endletdecode_single_objectdecoder:Ezjsone.value=letmoduleStack=structtypet=|In_arrayofEzjsone.valuelist*t|In_objectofstring*(string*Ezjsone.value)list*t|Emptyendinletloc()=Jsone.decoded_rangedecoderinletdec()=matchJsone.decodedecoderwith|`Lexemel->l|`Errore->raise(Abort(`Error(loc(),e)))|`End->raise(Abort`EOI)|`Await->assertfalseinletrecenterlstack=matchlwith|`Os->obj[]stack|_->raise(Abort(`Unexpected"decoding single object failed"))andvaluelstack=matchlwith|`Os->obj[]stack|`As->arr[]stack|(`Null|`Bool_|`String_|`Float_)asl->continuelstack|_->raise(Abort(`Unexpected"value"))andarrso_farstack=matchdec()with|`Ae->continue(`A(List.revso_far))stack|l->letstack=Stack.In_array(so_far,stack)invaluelstackandobjso_farstack=matchdec()with|`Oe->continue(`O(List.revso_far))stack|`Namen->letstack=Stack.In_object(n,so_far,stack)invalue(dec())stack|_->raise(Abort(`Unexpected"object fields"))andcontinuevstack=matchstackwith|Stack.In_array(vs,stack)->letso_far=v::vsinarrso_farstack|Stack.In_object(n,ms,stack)->letso_far=(n,v)::msinobjso_farstack|Stack.Empty->vinenter(dec())Emptyletencode_valueejson=letmoduleStack=structtypet=|In_arrayofEzjsone.valuelist*t|In_objectof(string*Ezjsone.value)list*t|Emptyendinletencel=ignore(Jsone.encodee(`Lexemel))inletrectvestack=matchvwith|`Avs->ence`As;arrvsestack|`Oms->ence`Os;objmsestackandvaluevestack=matchvwith|(`Null|`Bool_|`Float_|`String_)asv->encev;continueestack|#Ezjsone.tasx->t(x:>Ezjsone.t)estackandarrvsestack=matchvswith|v::vs'->letstack=Stack.In_array(vs',stack)invaluevestack|[]->ence`Ae;continueestackandobjmsestack=matchmswith|(n,v)::ms->ence(`Namen);letstack=Stack.In_object(ms,stack)invaluevestack|[]->ence`Oe;continueestackandcontinueestack=matchstackwith|Stack.In_array(vs,stack)->arrvsestack|Stack.In_object(ms,stack)->objmsestack|Stack.Empty->()invaluejsoneStack.Emptyletmap_geometryfsrcdst=letdecoder=Jsone.decodersrcinletencoder=Jsone.encoderdstinletloc()=Jsone.decoded_rangedecoderinletencv=matchJsone.encodeencodervwith|`Ok->()|`Partial->raise(Abort(`Unexpected"partial encoding"))inletrecgo()=matchJsone.decodedecoderwith(* TODO(patricoferris): A geometry collection could explode on us here... *)|`Lexeme(`Name"geometry"ast)->(matchG.of_json@@decode_single_objectdecoderwith|Error(`Msgm)->raise(Abort(`Unexpectedm))|Okv->(matchG.geojsonvwith|Geometryg->letg'=fginenc(`Lexemet);encode_valueencoder(G.to_json@@G.v?bbox:(G.bboxv)(Geometryg'));go()|_->raise(Invalid_argument"Expected a geometry object")))|`Lexeme_ast->enct;go()|`Errore->raise(Abort(`Error(loc(),e)))|`End->ignore@@Jsone.encodeencoder`End|`Await->assertfalseintryOk(go())withAborte->Erroreletmap_propsfsrcdst=letdecoder=Jsone.decodersrcinletencoder=Jsone.encoderdstinletloc()=Jsone.decoded_rangedecoderinletencv=matchJsone.encodeencodervwith|`Ok->()|`Partial->raise(Abort(`Unexpected"partial encoding"))inletrecgo()=matchJsone.decodedecoderwith|`Lexeme(`Name"properties"ast)->leto=f@@decode_single_objectdecoderinenc(`Lexemet);encode_valueencodero;go()|`Lexeme_ast->enct;go()|`Errore->raise(Abort(`Error(loc(),e)))|`End->ignore@@Jsone.encodeencoder`End|`Await->assertfalseintryOk(go())withAborte->Erroreletfold_geometryfinitsrc=letdecoder=Jsone.decodersrcinletloc()=Jsone.decoded_rangedecoderinletrecgoacc=matchJsone.decodedecoderwith|`Lexeme(`Name"geometry")->(matchG.of_json@@decode_single_objectdecoderwith|Error(`Msgm)->raise(Abort(`Unexpectedm))|Okv->(matchG.geojsonvwith|Geometryg->letacc=faccgingoacc|_->raise(Invalid_argument"Expected a geometry object")))|`Lexeme_->goacc|`Errore->raise(Abort(`Error(loc(),e)))|`End->acc|`Await->assertfalseintryOk(goinit)withAborte->Erroreletfold_propsfinitsrc=letdecoder=Jsone.decodersrcinletloc()=Jsone.decoded_rangedecoderinletrecgoacc=match Jsone.decodedecoderwith|`Lexeme(`Name"properties")->letacc'=facc@@decode_single_objectdecoderingoacc'|`Lexeme_->goacc|`Errore->raise(Abort(`Error(loc(),e)))|`End->acc|`Await->assertfalseintryOk(goinit)withAborte->Erroreletiter_geometryfsrc=letdecoder=Jsone.decodersrcinletloc()=Jsone.decoded_rangedecoderinletrecgo()=matchJsone.decodedecoderwith|`Lexeme(`Name"geometry")->(matchG.of_json@@decode_single_objectdecoderwith|Error(`Msgm)->raise(Abort(`Unexpectedm))|Okg->fg;go())|`Lexeme_->go()|`Errore->raise(Abort(`Error(loc(),e)))|`End->()|`Await->assertfalseintryOk(go())withAborte->Erroreletiter_propsfsrc=letdecoder=Jsone.decodersrcinletloc()=Jsone.decoded_rangedecoderinletrecgo()=matchJsone.decodedecoderwith|`Lexeme(`Name"properties")->f@@decode_single_objectdecoder;go()|`Lexeme_->go()|`Errore->raise(Abort(`Error(loc(),e)))|`End->()|`Await->assertfalseintryOk(go())withAborte->ErroremoduleEzjsone=EzjsonemoduleJsone=JsonemoduleUutfe=Uutfe