Fix script (again)

This commit is contained in:
Robin van der Linde 2022-07-19 12:09:04 +02:00
parent 983ada79f3
commit e312253354
Signed by untrusted user: Robin-van-der-Linde
GPG key ID: 53956B3252478F0D

View file

@ -44,22 +44,36 @@ function renameTags(item): GeoJsonProperties {
} }
function convertTypes(properties: GeoJsonProperties): GeoJsonProperties { function convertTypes(properties: GeoJsonProperties): GeoJsonProperties {
for (const property in properties) { // Split the tags into a list
// Determine the original tag by looking at the value in the names table let tags = properties.tags.split(";");
const originalTag = Object.keys(Constants.names).find(
(tag) => Constants.names[tag] === property for (const tag in tags) {
// Split the tag into key and value
const key = tags[tag].split("=")[0];
const value = tags[tag].split("=")[1];
const originalKey = Object.keys(Constants.names).find(
(tag) => Constants.names[tag] === key
); );
// Check if we need to convert the value
if (Constants.types[originalTag]) { if (Constants.types[originalKey]) {
switch (Constants.types[originalTag]) { // We need to convert the value to the correct type
let newValue;
switch (Constants.types[originalKey]) {
case "boolean": case "boolean":
properties[property] = properties[property] === "1" ? "yes" : "no"; newValue = value === "1" ? "yes" : "no";
break; break;
default: default:
newValue = value;
break; break;
} }
tags[tag] = `${key}=${newValue}`;
} }
} }
// Rejoin the tags
properties.tags = tags.join(";");
// Return the properties
return properties; return properties;
} }
@ -70,14 +84,26 @@ function convertTypes(properties: GeoJsonProperties): GeoJsonProperties {
* @returns The properties with units added * @returns The properties with units added
*/ */
function addUnits(properties: GeoJsonProperties): GeoJsonProperties { function addUnits(properties: GeoJsonProperties): GeoJsonProperties {
for (const property in properties) { // Split the tags into a list
let tags = properties.tags.split(";");
for (const tag in tags) {
const key = tags[tag].split("=")[0];
const value = tags[tag].split("=")[1];
const originalKey = Object.keys(Constants.names).find(
(tag) => Constants.names[tag] === key
);
// Check if the property needs units, and doesn't already have them // Check if the property needs units, and doesn't already have them
if (Constants.units[property] && property.match(/.*([A-z]).*/gi) === null) { if (Constants.units[originalKey] && value.match(/.*([A-z]).*/gi) === null) {
properties[ tags[tag] = `${key}=${value} ${Constants.units[originalKey]}`;
property
] = `${properties[property]} ${Constants.units[property]}`;
} }
} }
// Rejoin the tags
properties.tags = tags.join(";");
// Return the properties
return properties; return properties;
} }
@ -150,7 +176,7 @@ function main(args: string[]): void {
properties = convertTypes(properties); properties = convertTypes(properties);
// Add units if necessary // Add units if necessary
addUnits(properties); properties = addUnits(properties);
// Add Maproulette tags // Add Maproulette tags
properties = addMaprouletteTags(properties, item); properties = addMaprouletteTags(properties, item);
@ -176,15 +202,14 @@ function main(args: string[]): void {
features: items, features: items,
}; };
// Output the data to the console // Write the data to a file or output to the console
console.log(JSON.stringify(featureCollection));
// Write the data to a file
if (output) { if (output) {
writeFileSync( writeFileSync(
`${output}.geojson`, `${output}.geojson`,
JSON.stringify(featureCollection, null, 2) JSON.stringify(featureCollection, null, 2)
); );
} else {
console.log(JSON.stringify(featureCollection));
} }
} }