forked from MapComplete/MapComplete
		
	More validation and fixes
This commit is contained in:
		
							parent
							
								
									f10e7f008f
								
							
						
					
					
						commit
						4766f8bbde
					
				
					 13 changed files with 192 additions and 207 deletions
				
			
		| 
						 | 
				
			
			@ -8,6 +8,7 @@ import {error} from "util";
 | 
			
		|||
import * as licenses from "../assets/generated/license_info.json"
 | 
			
		||||
import SmallLicense from "../Models/smallLicense";
 | 
			
		||||
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
 | 
			
		||||
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";
 | 
			
		||||
// This scripts scans 'assets/layers/*.json' for layer definition files and 'assets/themes/*.json' for theme definition files.
 | 
			
		||||
// It spits out an overview of those to be used to load them
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -28,7 +29,7 @@ const layerFiles = ScriptUtils.readDirRecSync("./assets/layers")
 | 
			
		|||
            console.error("Could not parse file ", path, "due to ", e)
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
const themeFiles : any[] = ScriptUtils.readDirRecSync("./assets/themes")
 | 
			
		||||
const themeFiles: any[] = ScriptUtils.readDirRecSync("./assets/themes")
 | 
			
		||||
    .filter(path => path.indexOf(".json") > 0)
 | 
			
		||||
    .filter(path => path.indexOf("license_info.json") < 0)
 | 
			
		||||
    .map(path => {
 | 
			
		||||
| 
						 | 
				
			
			@ -49,57 +50,62 @@ for (const i in licenses) {
 | 
			
		|||
}
 | 
			
		||||
const knownPaths = new Set<string>(licensePaths)
 | 
			
		||||
 | 
			
		||||
function validateLayer(layer: LayerConfig, context?:string) : number{
 | 
			
		||||
function validateLayer(layerJson: LayerConfigJson, context?: string): number {
 | 
			
		||||
    let errorCount = 0;
 | 
			
		||||
    const images = Array.from(layer.ExtractImages())
 | 
			
		||||
    const remoteImages = images.filter(img => img.indexOf("http") == 0)
 | 
			
		||||
    for (const remoteImage of remoteImages) {
 | 
			
		||||
        console.error("Found a remote image:", remoteImage, "in layer", layer.id)
 | 
			
		||||
    if (layerJson["overpassTags"] !== undefined) {
 | 
			
		||||
        errorCount++
 | 
			
		||||
        console.error("CRIT! Layer ", layerJson.id, "still uses the old 'overpassTags'-format. Please use 'source: {osmTags: <tags>}' instead")
 | 
			
		||||
    }
 | 
			
		||||
    for (const image of images) {
 | 
			
		||||
        if (!knownPaths.has(image)) {
 | 
			
		||||
            console.error("Image with path", image, "not found or not attributed; it is used in", layer.id, context === undefined ? "" : ` in a layer defined in the theme ${context}`)
 | 
			
		||||
    try {
 | 
			
		||||
        const layer = new LayerConfig(layerJson, "test", true)
 | 
			
		||||
        const images = Array.from(layer.ExtractImages())
 | 
			
		||||
        const remoteImages = images.filter(img => img.indexOf("http") == 0)
 | 
			
		||||
        for (const remoteImage of remoteImages) {
 | 
			
		||||
            console.error("Found a remote image:", remoteImage, "in layer", layer.id)
 | 
			
		||||
            errorCount++
 | 
			
		||||
        }
 | 
			
		||||
        for (const image of images) {
 | 
			
		||||
            if (!knownPaths.has(image)) {
 | 
			
		||||
                console.error("Image with path", image, "not found or not attributed; it is used in", layer.id, context === undefined ? "" : ` in a layer defined in the theme ${context}`)
 | 
			
		||||
                errorCount++
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
       
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        console.error("Layer ", layerJson.id ?? JSON.stringify(layerJson).substring(0, 50), " is invalid: ", e)
 | 
			
		||||
        return 1
 | 
			
		||||
    }
 | 
			
		||||
    return errorCount
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
let layerErrorCount = 0
 | 
			
		||||
const knownLayerIds = new Set<string>();
 | 
			
		||||
for (const layerFile of layerFiles) {
 | 
			
		||||
    knownLayerIds.add(layerFile.id)
 | 
			
		||||
    try {
 | 
			
		||||
        const layer = new LayerConfig(layerFile, "test", true)
 | 
			
		||||
       layerErrorCount += validateLayer(layer)
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        console.error("Layer ", layerFile.id, " is invalid: ", e)
 | 
			
		||||
        layerErrorCount++
 | 
			
		||||
    }
 | 
			
		||||
    if (layerFile.overpassTags !== undefined) {
 | 
			
		||||
        layerErrorCount++
 | 
			
		||||
        console.warn("Layer ", layerFile.id, "still uses the old 'overpassTags'-format. Please use 'source: {osmTags: <tags>}' instead")
 | 
			
		||||
    }
 | 
			
		||||
    layerErrorCount += validateLayer(layerFile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
let themeErrorCount = 0
 | 
			
		||||
for (const themeFile of themeFiles) {
 | 
			
		||||
 | 
			
		||||
    for (const layer of themeFile.layers) {
 | 
			
		||||
        if(typeof layer === "string"){
 | 
			
		||||
            if(!knownLayerIds.has(layer)){
 | 
			
		||||
        if (typeof layer === "string") {
 | 
			
		||||
            if (!knownLayerIds.has(layer)) {
 | 
			
		||||
                console.error("Unknown layer id: ", layer)
 | 
			
		||||
                themeErrorCount++
 | 
			
		||||
            }
 | 
			
		||||
        }else if(layer.builtin === undefined){
 | 
			
		||||
            // layer.builtin contains layer overrides - we can skip those
 | 
			
		||||
            layerErrorCount += validateLayer(layer, themeFile.id)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    themeFile.layers = themeFile.layers.filter(l => typeof l != "string")
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        const layout = new LayoutConfig(themeFile, true, "test")
 | 
			
		||||
        for (const layer of layout.layers) {
 | 
			
		||||
            layerErrorCount += validateLayer(layer, layout.id)
 | 
			
		||||
        const theme = new LayoutConfig(themeFile, true, "test")
 | 
			
		||||
        if(theme.id !== theme.id.toLowerCase()){
 | 
			
		||||
            console.error("Theme ids should be in lowercase, but it is ", theme.id)
 | 
			
		||||
        }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        console.error("Could not parse theme", themeFile["id"], "due to", e)
 | 
			
		||||
| 
						 | 
				
			
			@ -107,4 +113,4 @@ for (const themeFile of themeFiles) {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
console.log("Found ", layerErrorCount, "errors in the layers; "+themeErrorCount+" errors in the themes")
 | 
			
		||||
console.log("Found ", layerErrorCount, "errors in the layers; " + themeErrorCount + " errors in the themes")
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue