|
1 | 1 | package org.caseontology.examples;
|
2 | 2 |
|
| 3 | +import org.apache.jena.query.QueryExecution; |
| 4 | +import org.apache.jena.query.QueryExecutionFactory; |
| 5 | +import org.apache.jena.query.QueryFactory; |
| 6 | +import org.apache.jena.query.QuerySolution; |
| 7 | +import org.apache.jena.rdf.model.Model; |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.apache.jena.graph.Node; |
| 18 | +import org.apache.jena.query.Query; |
| 19 | +import org.apache.jena.query.ResultSet; |
| 20 | +import org.apache.jena.rdf.model.ModelFactory; |
| 21 | +import org.apache.jena.riot.Lang; |
| 22 | +import org.apache.jena.riot.RDFDataMgr; |
| 23 | +import org.caseontology.examples.geojson.Feature; |
| 24 | +import org.caseontology.examples.geojson.GeoRecord; |
| 25 | +import org.caseontology.examples.geojson.Geometry; |
| 26 | +import org.caseontology.examples.geojson.Properties; |
| 27 | +import org.caseontology.examples.geojson.Root; |
| 28 | + |
| 29 | +import com.google.gson.Gson; |
| 30 | +import com.google.gson.GsonBuilder; |
| 31 | + |
3 | 32 | public class GeoReader {
|
4 | 33 |
|
5 | 34 | private String InputPath;
|
6 | 35 |
|
7 | 36 | public GeoReader(String inputPath) {
|
8 | 37 | this.InputPath = inputPath;
|
9 | 38 | }
|
| 39 | + |
| 40 | + /** |
| 41 | + * The primary entrypoint for the GeoReader class. This method will read the RDF graph from the input |
| 42 | + * file and convert it to GeoJSON string. |
| 43 | + * |
| 44 | + * @return the GeoJSON string generated from the input file |
| 45 | + */ |
| 46 | + public String run() { |
| 47 | + |
| 48 | + // Define the SPARQL query to execute to retrieve the location information from |
| 49 | + // the input file |
| 50 | + String queryString = """ |
| 51 | + PREFIX uco-core: <https://ontology.unifiedcyberontology.org/uco/core/> |
| 52 | + PREFIX uco-location: <https://ontology.unifiedcyberontology.org/uco/location/> |
| 53 | +
|
| 54 | + SELECT ?lLatitude ?lLongitude ?lAddressType ?lCountry ?lLocality ?lPostalCode ?lRegion ?lStreet |
| 55 | + WHERE |
| 56 | + { |
| 57 | + ?nLocation a uco-location:Location . |
| 58 | + OPTIONAL |
| 59 | + { |
| 60 | + ?nLocation uco-core:hasFacet ?nLatLongFacet . |
| 61 | + ?nLatLongFacet a uco-location:LatLongCoordinatesFacet . |
| 62 | + OPTIONAL { ?nLatLongFacet uco-location:latitude ?lLatitude . } |
| 63 | + OPTIONAL { ?nLatLongFacet uco-location:longitude ?lLongitude . } |
| 64 | + } |
| 65 | +
|
| 66 | + OPTIONAL { |
| 67 | + ?nLocation uco-core:hasFacet ?nSimpleAddressFacet . |
| 68 | + ?nSimpleAddressFacet a uco-location:SimpleAddressFacet . |
| 69 | + OPTIONAL { ?nSimpleAddressFacet uco-location:addressType ?lAddressType . } |
| 70 | + OPTIONAL { ?nSimpleAddressFacet uco-location:country ?lCountry . } |
| 71 | + OPTIONAL { ?nSimpleAddressFacet uco-location:locality ?lLocality . } |
| 72 | + OPTIONAL { ?nSimpleAddressFacet uco-location:postalCode ?lPostalCode . } |
| 73 | + OPTIONAL { ?nSimpleAddressFacet uco-location:region ?lRegion . } |
| 74 | + OPTIONAL { ?nSimpleAddressFacet uco-location:street ?lStreet . } |
| 75 | + } |
| 76 | + } |
| 77 | + """; |
| 78 | + |
| 79 | + try { |
| 80 | + // Load the input file into an RDF model |
| 81 | + Model model = getModel(this.InputPath); |
| 82 | + |
| 83 | + // Execute the SPARQL query on the RDF model |
| 84 | + ResultSet result = executeSparqlQuery(model, queryString); |
| 85 | + |
| 86 | + // Convert the SPARQL query result to a list of GeoRecord objects |
| 87 | + List<GeoRecord> records = resultToObjects(result); |
| 88 | + |
| 89 | + // Convert the list of GeoRecord objects to a GeoJSON string and return the string |
| 90 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 91 | + Root geoJSON = recordsToGeoJSON(records); |
| 92 | + return gson.toJson(geoJSON); |
| 93 | + } catch (IOException e) { |
| 94 | + System.out.println("Error processing input: " + e.getMessage()); |
| 95 | + e.printStackTrace(); |
| 96 | + System.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + // This line will never be reached but the compiler complains if it is not explicitly returning a String value |
| 100 | + return ""; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates an RDF model from the provided input file. |
| 105 | + * |
| 106 | + * @param inputPath the path to the input file |
| 107 | + * @return Model the RDF graph built from the input file |
| 108 | + * @throws IOException if the input file does not exist or cannot be read |
| 109 | + */ |
| 110 | + private static Model getModel(String inputPath) throws IOException { |
| 111 | + Model model = ModelFactory.createDefaultModel(); |
| 112 | + |
| 113 | + InputStream inputStream = new FileInputStream(inputPath); |
| 114 | + RDFDataMgr.read(model, inputStream, Lang.JSONLD); |
| 115 | + |
| 116 | + return model; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Executes a SPARQL query on the provided RDF model. |
| 121 | + * |
| 122 | + * @param model the RDF model to query |
| 123 | + * @param sparqlQuery the SPARQL query to execute |
| 124 | + */ |
| 125 | + private static ResultSet executeSparqlQuery(Model model, String sparqlQuery) { |
| 126 | + Query query = QueryFactory.create(sparqlQuery); |
| 127 | + QueryExecution queryExecution = QueryExecutionFactory.create(query, model); |
| 128 | + return queryExecution.execSelect(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Converts the result of a SPARQL query to a list of GeoRecord objects. |
| 133 | + * |
| 134 | + * @param resultSet the result of a SPARQL query to be converted |
| 135 | + * @return List<GeoRecord> a list of GeoRecord objects |
| 136 | + */ |
| 137 | + private static List<GeoRecord> resultToObjects(ResultSet resultSet) { |
| 138 | + // Initialize the list of geo Record objects |
| 139 | + List<GeoRecord> records = new ArrayList<GeoRecord>(); |
| 140 | + |
| 141 | + while (resultSet.hasNext()) { |
| 142 | + QuerySolution soln = resultSet.nextSolution(); |
| 143 | + |
| 144 | + // Initialize a new Record object |
| 145 | + GeoRecord record = new GeoRecord(); |
| 146 | + // Get an iterator over the property names in the result set |
| 147 | + Iterator<String> names = soln.varNames(); |
| 148 | + // For each property in the result set, strip the "l" prefix from the property |
| 149 | + // name and set the value of the corresponding property on the Record object |
| 150 | + while (names.hasNext()) { |
| 151 | + String property = names.next(); |
| 152 | + String propertyName = property.substring(1); |
| 153 | + Node node = soln.get(property).asNode(); |
| 154 | + if (node != null) { |
| 155 | + String value = node.getLiteralLexicalForm(); |
| 156 | + try { |
| 157 | + Field field = record.getClass().getDeclaredField(propertyName); |
| 158 | + field.set(record, value); |
| 159 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 160 | + System.out.println( |
| 161 | + "Error setting property " + propertyName + " on Record object: " + e.getMessage()); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Add the record to the list of records to be returned |
| 167 | + records.add(record); |
| 168 | + } |
| 169 | + |
| 170 | + return records; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Converts a list of GeoRecord objects to a GeoJSON object. |
| 175 | + * |
| 176 | + * @param records the list of GeoRecord objects to be converted |
| 177 | + * @return Root the GeoJSON object |
| 178 | + */ |
| 179 | + private static Root recordsToGeoJSON(List<GeoRecord> records) { |
| 180 | + Root root = new Root(); |
| 181 | + root.type = "FeatureCollection"; |
| 182 | + |
| 183 | + for (GeoRecord record : records) { |
| 184 | + Feature feature = new Feature(); |
| 185 | + feature.type = "Feature"; |
| 186 | + feature.properties = new Properties(); |
| 187 | + feature.properties.addressType = record.AddressType; |
| 188 | + feature.properties.country = record.Country; |
| 189 | + feature.properties.locality = record.Locality; |
| 190 | + feature.properties.postalCode = record.PostalCode; |
| 191 | + feature.properties.region = record.Region; |
| 192 | + feature.properties.street = record.Street; |
| 193 | + // The GeoJSON format expects coordinates in the format [longitude, latitude] |
| 194 | + // Only add the coordinates if the latitude and longitude are not null |
| 195 | + if (record.Latitude != null && record.Longitude != null) { |
| 196 | + feature.geometry = new Geometry(); |
| 197 | + feature.geometry.coordinates = Arrays.asList(Double.parseDouble(record.Longitude), |
| 198 | + Double.parseDouble(record.Latitude)); |
| 199 | + } |
| 200 | + |
| 201 | + // Add the feature to the list of features in the GeoJSON object |
| 202 | + root.features.add(feature); |
| 203 | + } |
| 204 | + |
| 205 | + return root; |
| 206 | + } |
10 | 207 | }
|
0 commit comments