|
| 1 | +package org.codelibs.elasticsearch.df.content.geojson; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.OutputStreamWriter; |
| 8 | +import java.io.Writer; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | +import org.codelibs.elasticsearch.df.content.ContentType; |
| 16 | +import org.codelibs.elasticsearch.df.content.DataContent; |
| 17 | +import org.codelibs.elasticsearch.df.util.JsonUtils; |
| 18 | +import org.codelibs.elasticsearch.df.util.RequestUtil; |
| 19 | +import org.codelibs.elasticsearch.df.util.StringUtils; |
| 20 | +import org.elasticsearch.ElasticsearchException; |
| 21 | +import org.elasticsearch.action.ActionListener; |
| 22 | +import org.elasticsearch.action.search.SearchResponse; |
| 23 | +import org.elasticsearch.client.Client; |
| 24 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 25 | +import org.elasticsearch.common.xcontent.XContentType; |
| 26 | +import org.elasticsearch.rest.RestChannel; |
| 27 | +import org.elasticsearch.rest.RestRequest; |
| 28 | +import org.elasticsearch.search.SearchHit; |
| 29 | +import org.elasticsearch.search.SearchHits; |
| 30 | + |
| 31 | +import com.google.gson.Gson; |
| 32 | +import com.google.gson.GsonBuilder; |
| 33 | +import com.google.gson.JsonArray; |
| 34 | +import com.google.gson.JsonElement; |
| 35 | +import com.google.gson.JsonObject; |
| 36 | +import com.google.gson.JsonParser; |
| 37 | + |
| 38 | +public class GeoJsonContent extends DataContent { |
| 39 | + private static final Logger logger = LogManager.getLogger(GeoJsonContent.class); |
| 40 | + |
| 41 | + private final String geometryCoordinatesLonField; |
| 42 | + private final String geometryCoordinatesLatField; |
| 43 | + private final String geometryCoordinatesAltField; |
| 44 | + private final String geometryTypeField; |
| 45 | + private final String geometryCoordinatesField; |
| 46 | + private final boolean geometryKeepGeoInfo; |
| 47 | + private final List<String> excludeFields; |
| 48 | + |
| 49 | + public GeoJsonContent(final Client client, final RestRequest request, final ContentType contentType) { |
| 50 | + super(client, request, contentType); |
| 51 | + |
| 52 | + geometryCoordinatesLonField = request.param("geometry.lon_field",StringUtils.EMPTY_STRING); |
| 53 | + geometryCoordinatesLatField = request.param("geometry.lat_field",StringUtils.EMPTY_STRING); |
| 54 | + geometryCoordinatesAltField = request.param("geometry.alt_field",StringUtils.EMPTY_STRING); |
| 55 | + geometryTypeField = request.param("geometry.type_field",StringUtils.EMPTY_STRING); |
| 56 | + geometryCoordinatesField = request.param("geometry.coord_field",StringUtils.EMPTY_STRING); |
| 57 | + geometryKeepGeoInfo = request.paramAsBoolean("keep_geometry_info",false); |
| 58 | + |
| 59 | + final String[] fields = request.paramAsStringArray("exclude_fields", StringUtils.EMPTY_STRINGS); |
| 60 | + if (fields.length == 0) { |
| 61 | + excludeFields = new ArrayList<>(); |
| 62 | + } else { |
| 63 | + final List<String> fieldList = new ArrayList<>(); |
| 64 | + for (final String field : fields) { |
| 65 | + fieldList.add(field.trim()); |
| 66 | + } |
| 67 | + excludeFields = Collections.unmodifiableList(fieldList); |
| 68 | + } |
| 69 | + |
| 70 | + if (logger.isDebugEnabled()) { |
| 71 | + logger.debug("geometryTypeField: {}, geometryCoordinatesField: {}, geometryCoordinatesLonField: {}, " + |
| 72 | + "geometryCoordinatesLatField: {}, geometryCoordinatesAltField: {}, geometryKeepGeoInfo: {}, excludeFields: {}", |
| 73 | + geometryTypeField, geometryCoordinatesField, geometryCoordinatesLonField, |
| 74 | + geometryCoordinatesLatField, geometryCoordinatesAltField, geometryKeepGeoInfo, excludeFields); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void write(final File outputFile, final SearchResponse response, final RestChannel channel, |
| 80 | + final ActionListener<Void> listener) { |
| 81 | + try { |
| 82 | + final OnLoadListener onLoadListener = new OnLoadListener( |
| 83 | + outputFile, listener); |
| 84 | + onLoadListener.onResponse(response); |
| 85 | + } catch (final Exception e) { |
| 86 | + listener.onFailure(new ElasticsearchException("Failed to write data.", |
| 87 | + e)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected class OnLoadListener implements ActionListener<SearchResponse> { |
| 92 | + protected ActionListener<Void> listener; |
| 93 | + |
| 94 | + protected Writer writer; |
| 95 | + |
| 96 | + protected File outputFile; |
| 97 | + |
| 98 | + private long currentCount = 0; |
| 99 | + |
| 100 | + private boolean firstLine = true; |
| 101 | + |
| 102 | + protected OnLoadListener(final File outputFile, final ActionListener<Void> listener) { |
| 103 | + this.outputFile = outputFile; |
| 104 | + this.listener = listener; |
| 105 | + try { |
| 106 | + writer = new BufferedWriter(new OutputStreamWriter( |
| 107 | + new FileOutputStream(outputFile), "UTF-8")); |
| 108 | + } catch (final Exception e) { |
| 109 | + throw new ElasticsearchException("Could not open " |
| 110 | + + outputFile.getAbsolutePath(), e); |
| 111 | + } |
| 112 | + try { |
| 113 | + writer.append("{\"type\": \"FeatureCollection\", \"features\": ["); |
| 114 | + }catch (final Exception e) { |
| 115 | + onFailure(e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onResponse(final SearchResponse response) { |
| 121 | + final Gson gsonWriter = new GsonBuilder().create(); |
| 122 | + final String scrollId = response.getScrollId(); |
| 123 | + final SearchHits hits = response.getHits(); |
| 124 | + final int size = hits.getHits().length; |
| 125 | + currentCount += size; |
| 126 | + if (logger.isDebugEnabled()) { |
| 127 | + logger.debug("scrollId: {}, totalHits: {}, hits: {}, current: {}", |
| 128 | + scrollId, hits.getTotalHits(), size, currentCount); |
| 129 | + } |
| 130 | + try { |
| 131 | + for (final SearchHit hit : hits) { |
| 132 | + final String source = XContentHelper.convertToJson( |
| 133 | + hit.getSourceRef(), true, false, XContentType.JSON); |
| 134 | + if (!firstLine){ |
| 135 | + writer.append(','); |
| 136 | + }else{ |
| 137 | + firstLine = false; |
| 138 | + } |
| 139 | + |
| 140 | + final JsonElement propertiesJson = JsonParser.parseString(source); |
| 141 | + String geometryType = ""; |
| 142 | + |
| 143 | + JsonArray geometryCoordinates = new JsonArray(); |
| 144 | + if (!geometryCoordinatesField.isEmpty()){ |
| 145 | + JsonElement jsonEltCoord = JsonUtils.getJsonElement(propertiesJson,geometryCoordinatesField); |
| 146 | + if (jsonEltCoord !=null && !jsonEltCoord.isJsonNull()){ |
| 147 | + geometryCoordinates = jsonEltCoord.getAsJsonArray(); |
| 148 | + if (!geometryKeepGeoInfo){ |
| 149 | + JsonUtils.removeJsonElement(propertiesJson,geometryCoordinatesField); |
| 150 | + } |
| 151 | + } |
| 152 | + if (!geometryTypeField.isEmpty()){ |
| 153 | + JsonElement jsonEltType = JsonUtils.getJsonElement(propertiesJson,geometryTypeField); |
| 154 | + if (jsonEltType !=null && !jsonEltType.isJsonNull()){ |
| 155 | + geometryType = jsonEltType.getAsString(); |
| 156 | + if (!geometryKeepGeoInfo){ |
| 157 | + JsonUtils.removeJsonElement(propertiesJson,geometryTypeField); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + }else{ |
| 162 | + if (!geometryCoordinatesLonField.isEmpty() && !geometryCoordinatesLatField.isEmpty()){ |
| 163 | + JsonElement jsonEltLon = JsonUtils.getJsonElement(propertiesJson,geometryCoordinatesLonField); |
| 164 | + JsonElement jsonEltLat = JsonUtils.getJsonElement(propertiesJson,geometryCoordinatesLatField); |
| 165 | + if (jsonEltLon !=null && !jsonEltLon.isJsonNull() && jsonEltLat !=null && !jsonEltLat.isJsonNull()){ |
| 166 | + geometryCoordinates.add(jsonEltLon.getAsNumber()); |
| 167 | + geometryCoordinates.add(jsonEltLat.getAsNumber()); |
| 168 | + if (!geometryKeepGeoInfo) { |
| 169 | + JsonUtils.removeJsonElement(propertiesJson,geometryCoordinatesLonField); |
| 170 | + JsonUtils.removeJsonElement(propertiesJson,geometryCoordinatesLatField); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + if (!geometryCoordinatesAltField.isEmpty()){ |
| 175 | + JsonElement jsonElt = JsonUtils.getJsonElement(propertiesJson,geometryCoordinatesAltField); |
| 176 | + if (jsonElt !=null && !jsonElt.isJsonNull()){ |
| 177 | + geometryCoordinates.add(jsonElt.getAsNumber()); |
| 178 | + if (!geometryKeepGeoInfo) { |
| 179 | + JsonUtils.removeJsonElement(propertiesJson,geometryCoordinatesAltField); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + geometryType = "Point"; |
| 184 | + } |
| 185 | + |
| 186 | + for (String excludeField : excludeFields) { |
| 187 | + JsonUtils.removeJsonElement(propertiesJson,excludeField); |
| 188 | + } |
| 189 | + |
| 190 | + JsonObject geometryObject = new JsonObject(); |
| 191 | + geometryObject.addProperty("type", geometryType); |
| 192 | + geometryObject.add("coordinates", geometryCoordinates); |
| 193 | + |
| 194 | + JsonObject featureObject = new JsonObject(); |
| 195 | + featureObject.addProperty("type", "Feature"); |
| 196 | + featureObject.add("geometry", geometryObject); |
| 197 | + featureObject.add("properties", propertiesJson.getAsJsonObject()); |
| 198 | + |
| 199 | + writer.append('\n').append(gsonWriter.toJson(featureObject)); |
| 200 | + } |
| 201 | + |
| 202 | + if (size == 0 || scrollId == null) { |
| 203 | + // end |
| 204 | + writer.append('\n').append("]}"); |
| 205 | + writer.flush(); |
| 206 | + close(); |
| 207 | + listener.onResponse(null); |
| 208 | + } else { |
| 209 | + client.prepareSearchScroll(scrollId) |
| 210 | + .setScroll(RequestUtil.getScroll(request)) |
| 211 | + .execute(this); |
| 212 | + } |
| 213 | + } catch (final Exception e) { |
| 214 | + onFailure(e); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public void onFailure(final Exception e) { |
| 220 | + try { |
| 221 | + close(); |
| 222 | + } catch (final Exception e1) { |
| 223 | + // ignore |
| 224 | + } |
| 225 | + listener.onFailure(new ElasticsearchException("Failed to write data.", |
| 226 | + e)); |
| 227 | + } |
| 228 | + |
| 229 | + private void close() { |
| 230 | + if (writer != null) { |
| 231 | + try { |
| 232 | + writer.close(); |
| 233 | + } catch (final IOException e) { |
| 234 | + throw new ElasticsearchException("Could not close " |
| 235 | + + outputFile.getAbsolutePath(), e); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments