-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
192 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import togeojson from 'togeojson'; | ||
import Fetcher from '../Provider/Fetcher'; | ||
import Extent from '../Core/Geographic/Extent'; | ||
|
||
function getExtentFromGpxFile(file) { | ||
const bound = file.getElementsByTagName('bounds')[0]; | ||
if (bound) { | ||
const west = bound.getAttribute('minlon'); | ||
const east = bound.getAttribute('maxlon'); | ||
const south = bound.getAttribute('minlat'); | ||
const north = bound.getAttribute('maxlat'); | ||
return new Extent('EPSG:4326', west, east, south, north); | ||
} | ||
return new Extent('EPSG:4326', -180, 180, -90, 90); | ||
} | ||
|
||
class geoFileSource { | ||
constructor(source) { | ||
if (!source.url) { | ||
throw new Error('source.url is required'); | ||
} | ||
|
||
this.protocol = 'file'; | ||
|
||
this.url = source.url; | ||
|
||
this.tileMatrixSet = 'WGS84G'; | ||
|
||
// KML and GPX specifications all says that they should be in | ||
// EPSG:4326. We still support reprojection for them through this | ||
// configuration option | ||
this.projection = source.projection || 'EPSG:4326'; | ||
|
||
if (source.extent && !(source.extent instanceof Extent)) { | ||
this.extent = new Extent(this.projection, source.extent); | ||
} | ||
|
||
if (!source.zoom) { | ||
this.zoom = { min: 5, max: 21 }; | ||
} | ||
|
||
this.networkOptions = source.networkOptions || { crossOrigin: 'anonymous' }; | ||
|
||
this.whenReady = Fetcher.text(this.url, source.networkOptions).then((text) => { | ||
let geojson; | ||
const trimmedText = text.trim(); | ||
// We test the start of the string to choose a parser | ||
if (trimmedText.startsWith('<')) { | ||
// if it's an xml file, then it can be kml or gpx | ||
const parser = new DOMParser(); | ||
const file = parser.parseFromString(text, 'application/xml'); | ||
if (file.documentElement.tagName.toLowerCase() === 'kml') { | ||
geojson = togeojson.kml(file); | ||
} else if (file.documentElement.tagName.toLowerCase() === 'gpx') { | ||
geojson = togeojson.gpx(file); | ||
geojson.style.stroke = geojson.style.stroke || 'red'; | ||
// this.extent = source.extent.intersect(getExtentFromGpxFile(file)); // .as(layer.extent.crs())); | ||
this.extent = getExtentFromGpxFile(file); // .as(layer.extent.crs())); | ||
} else if (file.documentElement.tagName.toLowerCase() === 'parsererror') { | ||
throw new Error('Error parsing XML document'); | ||
} else { | ||
throw new Error('Unsupported xml file, only valid KML and GPX are supported, but no <gpx> or <kml> tag found.', | ||
file); | ||
} | ||
} else if (trimmedText.startsWith('{') || trimmedText.startsWith('[')) { | ||
geojson = JSON.parse(text); | ||
if (geojson.type !== 'Feature' && geojson.type !== 'FeatureCollection') { | ||
throw new Error('This json is not a GeoJSON'); | ||
} | ||
} else { | ||
throw new Error('Unsupported file: only well-formed KML, GPX or GeoJSON are supported'); | ||
} | ||
|
||
this.geojson = geojson; | ||
}); | ||
} | ||
|
||
extentInsideLimit(extent, target, parent, pitch, zoom) { | ||
const result = zoom >= this.zoom.min && zoom <= this.zoom.max && ((this.extent !== undefined && this.extent.intersectsExtent(extent[0])) || this.extent === undefined); | ||
return result; | ||
} | ||
} | ||
|
||
export default geoFileSource; |