Skip to content

Commit

Permalink
feat: Try to find coordinate system in output_config.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
billyc committed Jul 27, 2021
1 parent e941f07 commit 5ce8766
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 39 deletions.
15 changes: 6 additions & 9 deletions src/js/Coords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ function toLngLat(projection: string, p: any) {
* @returns EPSG code in "EPSG:1234" format
*/
function guessProjection(definition: string) {
const favoriteEPSG = ['31468', '25832', '25833', '2048', '4326', '26910']
const favoriteEPSG = ['31468', '25832', '25833', '2048', '26910', '4326']

const lookups = {
DHDN_3_degree_Gauss_Kruger_zone_4: 'EPSG:31468',
NAD_1983_UTM_Zone_10N: 'EPSG:26910',
}

console.log(definition)
// Simple EPSG:xxxx code? Just return it
const epsg = /^EPSG:\d+$/
if (epsg.test(definition)) return definition

// maybe a DHDN GK4 is there
for (const [key, epsg] of Object.entries(lookups)) {
if (definition.indexOf(key) > -1) return epsg
}

// Authority mentioned? Use it
const authority = /AUTHORITY\["EPSG","\d+"\]/g
let matches = ''
Expand All @@ -74,13 +78,6 @@ function guessProjection(definition: string) {
}
}

// maybe a DHDN GK4 is there
for (const [key, epsg] of Object.entries(lookups)) {
console.log(key, epsg)
console.log(definition.indexOf(key))
if (definition.indexOf(key) > -1) return epsg
}

// all else fails: return EPSG:31468
return ''
}
Expand Down
115 changes: 87 additions & 28 deletions src/plugins/transit-demand/TransitDemand.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,42 +285,101 @@ class MyComponent extends Vue {
15 + this.myState.yamlConfig.indexOf('transitSchedule')
)
const { files } = await this.myState.fileApi.getDirectory(this.myState.subfolder)
// Road network: first try the most obvious network filename:
let network = this.myState.yamlConfig.replaceAll('transitSchedule', 'network')
// if the obvious network file doesn't exist, just grab... the first network file:
if (files.indexOf(network) == -1) {
const allNetworks = files.filter(f => f.endsWith('network.xml.gz'))
if (allNetworks.length) network = allNetworks[0]
else {
this.loadingText = 'No road network found.'
network = ''
}
}
// Departures: use them if we are in an output folder (and they exist)
let demandFiles = [] as string[]
if (this.myState.yamlConfig.indexOf('output_transitSchedule') > -1) {
demandFiles = files.filter(f => f.endsWith('pt_stop2stop_departures.csv.gz'))
}
// Coordinates: oy
const projection = 'EPSG:31468'
this.vizDetails = {
transitSchedule: this.myState.yamlConfig,
network,
network: '',
title,
description: '',
demand: demandFiles.length ? demandFiles[0] : '',
projection,
demand: '',
projection: '',
}
this.$emit('title', title)
this.projection = this.vizDetails.projection
// go deeper if we are in full-screen
if (!this.thumbnail) {
const { files } = await this.myState.fileApi.getDirectory(this.myState.subfolder)
// Road network: first try the most obvious network filename:
let network = this.myState.yamlConfig.replaceAll('transitSchedule', 'network')
// if the obvious network file doesn't exist, just grab... the first network file:
if (files.indexOf(network) == -1) {
const allNetworks = files.filter(f => f.endsWith('network.xml.gz'))
if (allNetworks.length) network = allNetworks[0]
else {
this.loadingText = 'No road network found.'
network = ''
}
}
// Departures: use them if we are in an output folder (and they exist)
let demandFiles = [] as string[]
if (this.myState.yamlConfig.indexOf('output_transitSchedule') > -1) {
demandFiles = files.filter(f => f.endsWith('pt_stop2stop_departures.csv.gz'))
}
// Coordinates:
const projection = await this.guessProjection(files)
console.log(projection)
// Save everything
this.vizDetails.network = network
this.vizDetails.projection = projection
this.projection = this.vizDetails.projection
if (demandFiles.length) this.vizDetails.demand = demandFiles[0]
}
}
private async guessProjection(files: string[]) {
// 1. if we have it in storage already, use it
let savedConfig = localStorage.getItem(this.myState.yamlConfig) as any
if (savedConfig) {
try {
const { projection } = JSON.parse(savedConfig)
if (projection) return projection
else savedConfig = {}
} catch (e) {
console.error('bad saved config in storage', savedConfig)
savedConfig = {}
// fail! ok try something else
}
}
// 2. try to get it from config
const outputConfigs = files.filter(f => f.indexOf('output_config.xml') > -1)
if (outputConfigs.length && this.myState.fileSystem) {
console.log('trying to find CRS in', outputConfigs[0])
const fetcher = await XmlFetcher.create({
fileApi: this.myState.fileSystem.slug,
filePath: this.myState.subfolder + '/' + outputConfigs[0],
})
const results = (await fetcher.fetchXML()) as any
fetcher.destroy()
try {
const global = results.config.module.filter((f: any) => f.$.name === 'global')[0]
const crs = global.param.filter((p: any) => p.$.name === 'coordinateSystem')[0]
const crsValue = crs.$.value
// save it
savedConfig = savedConfig || {}
savedConfig.projection = crsValue
const outputSettings = JSON.stringify(savedConfig)
localStorage.setItem(this.myState.yamlConfig, outputSettings)
return crsValue
} catch (e) {
console.error('Failed parsing output config XML')
}
}
// 3. ask the user
const entry = prompt('Need EPSG number:', '')
const projection = `EPSG:${entry}`
const outputSettings = JSON.stringify({ projection })
localStorage.setItem(this.myState.yamlConfig, outputSettings)
return projection
}
private async loadYamlConfig() {
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/transit-demand/TransitSupplyHelper.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InitParams, MethodNames } from './TransitSupplyHelperContract'
import { NetworkNode, TransitLine, RouteDetails } from './Interfaces'

import proj4 from 'proj4'
import EPSGdefinitions from 'epsg'

class TransitSupplyHelper extends AsyncBackgroundWorker {
private params!: InitParams
Expand Down Expand Up @@ -37,6 +38,10 @@ class TransitSupplyHelper extends AsyncBackgroundWorker {

/** Add various projections that we use here */
private addProj4Definitions() {
for (const [key, epsg] of Object.entries(EPSGdefinitions) as any[]) {
if (epsg) proj4.defs(key, epsg)
}

proj4.defs([
[
// south africa
Expand Down Expand Up @@ -82,8 +87,6 @@ class TransitSupplyHelper extends AsyncBackgroundWorker {
}

private convertCoords() {
console.log('projection is: ' + this.projection)

for (const id in this._network.nodes) {
if (this._network.nodes.hasOwnProperty(id)) {
const node: NetworkNode = this._network.nodes[id]
Expand All @@ -108,6 +111,8 @@ class TransitSupplyHelper extends AsyncBackgroundWorker {
transitRoutes: [],
}

if (!line.transitRoute) continue

for (const route of line.transitRoute) {
const details: RouteDetails = this.buildTransitRouteDetails(line.$.id, route)
details.uniqueRouteID = uniqueRouteID++
Expand All @@ -117,6 +122,7 @@ class TransitSupplyHelper extends AsyncBackgroundWorker {
// attr.transitRoutes.sort((a, b) => (a.id < b.id ? -1 : 1))
this._transitLines[attr.id] = attr
}

return {
data: {
network: this._network,
Expand Down

0 comments on commit 5ce8766

Please sign in to comment.