1
+ L . TileJSON = ( function ( ) {
2
+ var semver = "\\s*[v=]*\\s*([0-9]+)" // major
3
+ + "\\.([0-9]+)" // minor
4
+ + "\\.([0-9]+)" // patch
5
+ + "(-[0-9]+-?)?" // build
6
+ + "([a-zA-Z-][a-zA-Z0-9-\.:]*)?" ; // tag
7
+ var semverRegEx = new RegExp ( "^\\s*" + semver + "\\s*$" ) ;
8
+
9
+ var parse = function ( v ) {
10
+ return v . match ( semverRegEx ) ;
11
+ } ;
12
+
13
+ function defined ( o ) {
14
+ return ( typeof o !== "undefined" && o !== null ) ;
15
+ }
16
+
17
+ function validateVersion ( tileJSON ) {
18
+ if ( ! tileJSON . tilejson ) {
19
+ throw new Exception ( 'Missing property "tilejson".' ) ;
20
+ }
21
+
22
+ v = parse ( tileJSON . tilejson ) ;
23
+ if ( ! v || v [ 1 ] != 2 ) {
24
+ throw new Exception ( 'This parser supports version 2 '
25
+ + 'of TileJSON. (Provided version: "'
26
+ + tileJSON . tilejson + '"' ) ;
27
+ }
28
+ } ;
29
+
30
+ function parseZoom ( tileJSON , cfg ) {
31
+ if ( tileJSON . minzoom ) {
32
+ cfg . minZoom = parseInt ( tileJSON . minzoom ) ;
33
+ }
34
+
35
+ if ( tileJSON . maxzoom ) {
36
+ cfg . maxZoom = parseInt ( tileJSON . maxzoom ) ;
37
+ } else {
38
+ cfg . maxZoom = 22 ;
39
+ }
40
+
41
+ return cfg ;
42
+ }
43
+
44
+ function createMapConfig ( tileJSON , cfg ) {
45
+ validateVersion ( tileJSON ) ;
46
+
47
+ if ( ! defined ( cfg ) ) {
48
+ cfg = { } ;
49
+ }
50
+
51
+ parseZoom ( tileJSON , cfg ) ;
52
+
53
+ if ( tileJSON . center ) {
54
+ var center = tileJSON . center ;
55
+ cfg . center = new L . LatLng ( center [ 1 ] , center [ 0 ] ) ;
56
+ cfg . zoom = center [ 2 ] ;
57
+ }
58
+
59
+ if ( tileJSON . attribution ) {
60
+ cfg . attributionControl = true ;
61
+ }
62
+
63
+ if ( tileJSON . projection ) {
64
+ var t = tileJSON . transform ;
65
+ cfg . crs =
66
+ L . CRS . proj4js ( tileJSON . crs ,
67
+ tileJSON . projection ,
68
+ new L . Transformation ( t [ 0 ] , t [ 1 ] , t [ 2 ] , t [ 3 ] ) ) ;
69
+ // FIXME: This might not be true for all projections, actually
70
+ cfg . continuousWorld = true ;
71
+ }
72
+
73
+ if ( tileJSON . scales ) {
74
+ var s = tileJSON . scales ;
75
+ cfg . scale = function ( zoom ) {
76
+ return s [ zoom ] ;
77
+ }
78
+ }
79
+
80
+ return cfg ;
81
+ } ;
82
+
83
+ function createTileLayerConfig ( tileJSON , cfg ) {
84
+ validateVersion ( tileJSON ) ;
85
+
86
+ if ( ! defined ( cfg ) ) {
87
+ cfg = { } ;
88
+ }
89
+
90
+ parseZoom ( tileJSON , cfg ) ;
91
+
92
+ if ( tileJSON . attribution ) {
93
+ cfg . attribution = tileJSON . attribution ;
94
+ }
95
+
96
+ if ( tileJSON . scheme ) {
97
+ cfg . scheme = tileJSON . scheme ;
98
+ }
99
+
100
+ if ( tileJSON . projection ) {
101
+ // FIXME: This might not be true for all projections, actually
102
+ cfg . continuousWorld = true ;
103
+ }
104
+
105
+ return cfg ;
106
+ } ;
107
+
108
+
109
+
110
+
111
+ function createTileLayer ( tileJSON ) {
112
+ var tileUrl = tileJSON . tiles [ 0 ] . replace ( / \$ ( { [ s x y z ] } ) / g, '$1' ) ;
113
+ return new L . TileLayer ( tileUrl , createTileLayerConfig ( tileJSON ) ) ;
114
+ } ;
115
+
116
+ function createMap ( id , tileJSON , options ) {
117
+ var mapConfig ;
118
+ var tileLayerConfig ;
119
+
120
+ if ( defined ( options ) ) {
121
+ mapConfig = options . mapOptions ;
122
+ tileLayerConfig = options . tileLayerOptions ;
123
+ } else {
124
+ mapConfig = { } ;
125
+ tileLayerConfig = { } ;
126
+ }
127
+
128
+ var mapConfig = createMapConfig ( tileJSON , mapConfig ) ;
129
+ mapConfig . layers = [ createTileLayer ( tileJSON , tileLayerConfig ) ] ;
130
+ return new L . Map ( id , mapConfig ) ;
131
+ }
132
+
133
+ return {
134
+ createMapConfig : createMapConfig ,
135
+
136
+ createTileLayerConfig : createTileLayerConfig ,
137
+
138
+ createTileLayer : createTileLayer ,
139
+
140
+ createMap : createMap
141
+ }
142
+ } ( ) ) ;
0 commit comments