in json talking to ogc web services - meci · 2015. 10. 6. · talking to ogc web services in json...

41
Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1

Upload: others

Post on 15-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Talking to OGC Web Servicesin JSON

Or How I Learned to Stop Worryingand Love XML Processing in JavaScript

1

Page 2: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Hi, my name is …

Alexey Valikovhttp://xing.to/vahttps://github.com/highsource@orless

2

Page 3: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

… and I’m (also) into GIS and Web Mapping

GIS 2go

Internal apps for the Deutsche Bahn3

Page 4: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Web Mapping means JavaScript

4

Page 5: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

JavaScript speaks JSON{

"type": "Feature",

"geometry": {

"type": "Point",

"coordinates": [125.6, 10.1]

},

"properties": {

"name": "Dinagat Islands"

}

}

[ˈdʒeɪsən]

5

Page 6: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

GIS-Services are based on OGC Standards

6

Page 7: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

… specified by XML Schemas

More than 50 SchemasMore than 100 individual versionsOver 8MB and 800 individual XSD files

7

Page 9: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

JS Apps have a communication problem

JS App WFS Server

{”JS”:”ON”} ???

XSD

9

<x:ml/>

Page 10: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix provides a solution

Mapping

10

WFS Server

{”JS”:”ON”} Jsonix

XSD

<x:ml/>

JS App

Page 11: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

What is Jsonix?

Jsonix is a powerful Open-Source JavaScript libraryfor XML↔JS conversionhttps://github.com/highsource/jsonixBidirectional, type-safe, strongly-structuredDriven by declarative XML↔JS mappings…… which can be generated from XML schemas automatically

11

Page 12: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Further Jsonix features

Works in browsersWorks in Node.jsWorks with AMD, CommonJS and without (globals)Namespace-awareSupports almost all of the XML Schema simple types (including binary types and QNames)Supports xsi:typeAnd much more

12

Page 13: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix Example

Parse WMS Capabilities

JSFiddle:http://bit.do/jsonix-001

13

Page 14: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Parsing with Jsonix - Code Walk-Throughvar getCapabilitiesUrl = …;

// First we create a context for XLink and WMS mappingsvar context = new Jsonix.Context([XLink_1_0, WMS_1_3_0], …);

// Then we create an unmarshaller (parser)var unmarshaller = context.createUnmarshaller();

// And finally use this unmarshaller// to parse an XML document from the given URLunmarshaller.unmarshalURL(getCapabilitiesUrl, function(result) { // We’ll get results in a callback function $('#json').html(JSON.stringify(result, null, 2));});

14

Page 15: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix creates pretty JSON<WMS_Capabilities … version="1.3.0" updateSequence="163"> <Service> … </Service> <Capability> <Request> … </Request> <Exception> … </Exception> <Layer>

… <Layer queryable="1">…</Layer> <Layer queryable="1" opaque="0">…</Layer> … </Layer> </Capability></WMS_Capabilities>

{ "WMS_Capabilities": { "version": "1.3.0", "updateSequence": "163", "service": { … }, "capability": { "request": { … }, "exception": { … }, "layer": { … , "layer": [ … ] } } }}

15

Page 16: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

… and can serialize this JSON back to XML

// Create (or reuse) the Jsonix Contextvar context = new Jsonix.Context([XLink_1_0, WMS_1_3_0], …);

// Create a marshaller (serializer)var marshaller = context.createMarshaller();

// Serialize JSON as XML string or DOM node$('#xml').text(marshaller.marshalString(result));

16

Page 17: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

The same thing without Jsonix?

OpenLayers 3: ol.format.WMSCapabilities

JSFiddle:http://bit.do/jsonix-002

17

Page 18: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Let’s take a closer look into the OL3 code

ol.format.WMSCapabilities

ca. 28 kB, ca 0.8 KLoCType-safeStrongly-structuredOnly parsingWritten manuallySuper exciting code

18

Page 19: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Why yet another XML-JS tool?

19

Page 20: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonixis strongly-structured

and type-safe

And that’s unparalleled

20

Page 21: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

What does “strongly-structured” mean?<WMS_Capabilities … version="1.3.0" updateSequence="163"> <Service> … </Service> <Capability> <Request> … </Request> <Exception> … </Exception> <Layer> … <!-- Just one Layer element --> <Layer queryable="1"> … </Layer> </Layer> </Capability></WMS_Capabilities>

How should the Layer elementbe represented in JSON?As a single element or as an array?

capability.layer.layer?capability.layer.layer[0]?

You can’t decide it just based on the XML instanceYou have to know the schema

21

Page 22: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix is strongly-structuredJsonix knows the structure of the XML from the mapping

… and respects the defined cardinalities, element orders, properties, naming and so on

Jsonix always produces JSON and XML-structures which respect the definitions from the mapping

22

Page 23: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix produces reliable structures<WMS_Capabilities … version="1.3.0" updateSequence="163"> <Service> … </Service> <Capability> <Request> … </Request> <Exception> … </Exception> <Layer> … <Layer queryable="1"> … </Layer> </Layer> </Capability></WMS_Capabilities>

layer is an array:

capability.layer.layer[0]

Because the mapping says so:

23

Page 24: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Strong, reliable structures===

simpler code

24

Page 25: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

What does “type-safe” mean?<WMS_Capabilities … version="1.3.0" updateSequence="163"> <Service> … </Service> <Capability> <Request> … </Request> <Exception> … </Exception> <Layer> … <Layer queryable="1"> … </Layer> </Layer> </Capability></WMS_Capabilities>

Which type shouldqueryable=”1” have in JSON?

String? Number? Boolean?

You can’t decide it just based on the XML instanceYou have to know the schema

25

Page 26: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix is type-safeJsonix knows types of elements and attributes from the mapping

… and converts strings from/to these types automatically

Jsonix always produces values in JSON and XML according to the types defined in the mapping

26

Page 27: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix converts the types automatically <WMS_Capabilities … version="1.3.0" updateSequence="163"> <Service> … </Service> <Capability> <Request> … </Request> <Exception> … </Exception> <Layer> … <Layer queryable="1"> … </Layer> </Layer> </Capability></WMS_Capabilities>

queryable is a boolean:

{ "queryable" : true, … }

Because the mappings says so:

27

Page 28: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Safe, automatic type conversion===

less work

28

Page 29: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix uses declarative mappingsJsonix mappings are just descriptions of structures, not imperative programs

29

Page 30: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Mappings are essentialfor strong structures

and safe typing

30

Page 31: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix can generate mappings from XSDs

Jsonix Runtime

XSD

Mapping

Jsonix Schema Compiler

{“JS”:”ON”} <x:ml/>

31

Page 32: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

..as followsjava -jar jsonix-schema-compiler.jar

-catalog schemas/catalog.cat schemas/ogc/wms/1.3.0/capabilities_1_3_0.xsd -b schemas -d mappings

Generates Jsonix mappings for WMS 1.3.0:WMS_1_3_0_Full.js

GitHub:http://bit.do/jsonix-006

32

Page 33: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Experimental: Generate JSON Schemas from XSDs

Jsonix Runtime

XSD

Mapping

Jsonix Schema Compiler

{“JS”:”ON”} <x:ml/>

33

JSON Schema

Experimental java -jar jsonix-schema-compiler.jar -generateJsonSchema …

Page 34: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Experimental: Validate JSON against generated JSON Schemas

Using the great “Another JSON Schema Validator” AJV// Load JSON Schemas

var ajv = new Ajv();

ajv.addSchema(XMLSchemaJsonSchema, … ); ajv.addSchema(JsonixJsonSchema, … );

ajv.addSchema(XLink_1_0JsonSchema, 'http://www.w3.org/1999/xlink');

// Compile the validation function and validate

var validate = ajv.compile(WMS_1_3_0JsonSchema);

if (!validate(capabilitiesElement)) { console.log(validate.errors); }

34

[{keyword: 'required',dataPath: '.value.capability.request.getCapabilities.dcpType[\'0\'].http',message: 'property .http is required'

}, … ]

Page 35: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Compiling XSDs into mappings is hard

35

By A

lex E. P

roimos (http://w

ww

.flickr.com

/photos/proimos/4199675334/)

[CC

BY

2.0 (http://creativecomm

ons.org/licenses/by/2.0)],via W

ikimedia C

omm

ons

Page 36: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Compiling XSDs into mappings is hard

We’ve huge number of schemas with complex interdependenciesSchemas sometimes have errorsWe have to resolve naming collisionsWe’re often forced to customize generation or even patch schemas

36

Page 37: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

(Unofficial) OGC Schemas Project

https://github.com/highsource/ogc-schemasPre-generated mapping for OGC Schemas:JS↔XML: Jsonix mappingsJava↔XML: JAXB classesAvailable via npmjs.org and Maven Central

37

Page 38: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

(Unofficial) OGC Schemas ProjectCompiles over 30 OGC Schemas with more than 80 single versions

OWS, WMS, SLD, GML, Filter, WFS, WPS, WCS, WMC, SWE, SPS, SOS, OM, SensorML, KML, ISO 19139, CSW, and many more… … ready-to-use with Jsonix

38

Page 39: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Using pre-generated mappingsvar mappings = [XLink_1_0, SMIL_2_0, SMIL_2_0_Language, GML_3_1_1, OWS_1_0_0, Filter_1_1_0, DC_1_1, DCT, CSW_2_0_2];

// Create a Jsonix context

var context = new Jsonix.Context(mappings, {

namespacePrefixes: {

"http://www.opengis.net/cat/csw/2.0.2": "csw",

"http://www.opengis.net/ogc": "ogc",

"http://www.opengis.net/gml": "gml"

}

});

JSFiddle:http://bit.do/jsonix-007

39

Page 40: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

TakeawayJsonix is a powerful Open-Source JavaScript library for XML↔JS conversionWorks in browsers as well as Node.jsBidirectional, strongly-structured and type-safeUses declarative XML↔JS mappingsMappings can be generated from XML SchemasThe (unofficial) OGC Schemas Project provides ready-to-use pre-generated mapping for many OGC Schemas

40

Page 41: in JSON Talking to OGC Web Services - MECI · 2015. 10. 6. · Talking to OGC Web Services in JSON Or How I Learned to Stop Worrying and Love XML Processing in JavaScript 1. ... Jsonix

Jsonix can drastically simplifyXML processing in JavaScript apps

and thus save a lot of development effort

https://github.com/highsource/jsonix

41