|
| 1 | +import json |
| 2 | +import sys |
| 3 | +from os.path import exists, isdir, isfile |
| 4 | + |
| 5 | +from geotypes import GeoRecord |
| 6 | +from geoutilities import records_to_geojson, remove_nulls |
| 7 | +from rdflib import Graph |
| 8 | + |
| 9 | +# Parse the arguments from the CLI to get the input and output filenames |
| 10 | +if len(sys.argv) != 3: |
| 11 | + print("Usage: python case2geojson.py <input-file> <output-file>") |
| 12 | + sys.exit(1) |
| 13 | + |
| 14 | +input_filename: str = sys.argv[1] |
| 15 | +output_filename: str = sys.argv[2] |
| 16 | + |
| 17 | +# Ensure the input file exists |
| 18 | +if not exists(input_filename) and not isfile(input_filename): |
| 19 | + print(f"File not found: {input_filename}") |
| 20 | + sys.exit(1) |
| 21 | + |
| 22 | +# Ensure the output directory exists |
| 23 | +output_directory: str = output_filename[: output_filename.rfind("/")] |
| 24 | +if not exists(output_directory) and not isdir(output_directory): |
| 25 | + print(f"Directory not found: {output_directory}") |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | +# Build the rdflib graph from the input file |
| 29 | +graph: Graph = Graph() |
| 30 | +graph.parse(input_filename) |
| 31 | + |
| 32 | +# Write the SPARQL query to get the data from the graph |
| 33 | +query: str = """ |
| 34 | + SELECT ?lLatitude ?lLongitude ?lAddressType ?lCountry ?lLocality ?lPostalCode ?lRegion ?lStreet |
| 35 | + WHERE |
| 36 | + { |
| 37 | + ?nLocation a uco-location:Location . |
| 38 | + OPTIONAL |
| 39 | + { |
| 40 | + ?nLocation uco-core:hasFacet ?nLatLongFacet . |
| 41 | + ?nLatLongFacet a uco-location:LatLongCoordinatesFacet . |
| 42 | + OPTIONAL { ?nLatLongFacet uco-location:latitude ?lLatitude . } |
| 43 | + OPTIONAL { ?nLatLongFacet uco-location:longitude ?lLongitude . } |
| 44 | + } |
| 45 | +
|
| 46 | + OPTIONAL { |
| 47 | + ?nLocation uco-core:hasFacet ?nSimpleAddressFacet . |
| 48 | + ?nSimpleAddressFacet a uco-location:SimpleAddressFacet . |
| 49 | + OPTIONAL { ?nSimpleAddressFacet uco-location:addressType ?lAddressType . } |
| 50 | + OPTIONAL { ?nSimpleAddressFacet uco-location:country ?lCountry . } |
| 51 | + OPTIONAL { ?nSimpleAddressFacet uco-location:locality ?lLocality . } |
| 52 | + OPTIONAL { ?nSimpleAddressFacet uco-location:postalCode ?lPostalCode . } |
| 53 | + OPTIONAL { ?nSimpleAddressFacet uco-location:region ?lRegion . } |
| 54 | + OPTIONAL { ?nSimpleAddressFacet uco-location:street ?lStreet . } |
| 55 | + } |
| 56 | + } |
| 57 | + """ |
| 58 | + |
| 59 | +results = graph.query(query) |
| 60 | + |
| 61 | +# Define the list of GeoRecords |
| 62 | +records: list[GeoRecord] = [] |
| 63 | + |
| 64 | +# Loop through the results and add them to the list of GeoRecords if the latitude and longitude are present |
| 65 | +for row in results: |
| 66 | + geo_record: GeoRecord = GeoRecord() |
| 67 | + geo_record.Latitude = row.lLatitude |
| 68 | + geo_record.Longitude = row.lLongitude |
| 69 | + geo_record.AddressType = row.lAddressType |
| 70 | + geo_record.Country = row.lCountry |
| 71 | + geo_record.Locality = row.lLocality |
| 72 | + geo_record.PostalCode = row.lPostalCode |
| 73 | + geo_record.Region = row.lRegion |
| 74 | + geo_record.Street = row.lStreet |
| 75 | + records.append(geo_record) |
| 76 | + |
| 77 | +# Convert the data to a GeoJSON structured object |
| 78 | +geoJSON = records_to_geojson(records) |
| 79 | + |
| 80 | +# Remove null values from the GeoJSON object |
| 81 | +geoDict: dict = geoJSON.reprJSON() |
| 82 | +geoDict = remove_nulls(geoDict) |
| 83 | + |
| 84 | +# Write the GeoJSON object to the output file |
| 85 | +with open(output_filename, "w") as output_file: |
| 86 | + output_file.write(json.dumps(geoDict, indent=4)) |
0 commit comments