Refactor map layer importer.

- Now a layer may consist of a path pointing to a directory plus a
  glob, or a path pointing directly to a single file.
- If a file already exists in the database, check if the layer
  name has changed and if so, update it.
- Do not import the actual file contents, as the path is enough
  (it can be retrieved via the /file/:path API endpoint).
This commit is contained in:
D. Berge
2023-09-12 11:05:10 +02:00
parent b71489cee1
commit f9a70e0145

View File

@@ -18,6 +18,58 @@ import configuration
from datastore import Datastore
if __name__ == '__main__':
"""
Imports map layers from the directories defined in the configuration object
`import.map.layers`. The content of that key is an object with the following
structure:
{
layer1Name: [
format: "geojson",
path: "", // Logical path to a directory
globs: [
"**/*.geojson", // List of globs matching map data files
]
],
layer2Name: …
}
"""
def process (layer_name, layer, physical_filepath):
physical_filepath = str(physical_filepath)
logical_filepath = configuration.untranslate_path(physical_filepath)
print(f"Found {logical_filepath}")
if not db.file_in_db(logical_filepath):
age = time.time() - os.path.getmtime(physical_filepath)
if age < file_min_age:
print("Skipping file because too new", logical_filepath)
return
print("Importing")
file_info = {
"type": "map_layer",
"format": layer["format"],
"name": layer_name
}
db.save_file_data(logical_filepath, json.dumps(file_info))
else:
file_info = db.get_file_data(logical_filepath)
if file_info and file_info["name"] != layer_name:
print("Renaming to", layer_name)
file_info["name"] = layer_name
db.save_file_data(logical_filepath, json.dumps(file_info))
else:
print("Already in DB")
print("Reading configuration")
file_min_age = configuration.read().get('imports', {}).get('file_min_age', 10)
@@ -44,31 +96,14 @@ if __name__ == '__main__':
fileprefix = layer["path"]
realprefix = configuration.translate_path(fileprefix)
for globspec in layer["globs"]:
for physical_filepath in pathlib.Path(realprefix).glob(globspec):
physical_filepath = str(physical_filepath)
logical_filepath = configuration.untranslate_path(physical_filepath)
print(f"Found {logical_filepath}")
if os.path.isfile(realprefix):
if not db.file_in_db(logical_filepath):
process(layer_name, layer, realprefix)
age = time.time() - os.path.getmtime(physical_filepath)
if age < file_min_age:
print("Skipping file because too new", logical_filepath)
continue
else:
print("Importing")
file_info = {
"type": "map_layer",
"format": layer["format"],
"name": layer_name,
"data": pathlib.Path(physical_filepath).read_text()
}
db.save_file_data(logical_filepath, json.dumps(file_info))
else:
print("Already in DB")
for globspec in layer["globs"]:
for physical_filepath in pathlib.Path(realprefix).glob(globspec):
process(layer_name, layer, physical_filepath)
print("Done")