}
public void approveFile(AttachmentDescriptor attDescription) throws Exception {
DocumentDescriptor docDescriptor = attDescription.getDocumentDescriptor();
FileConversionContext approvedContext = docDescriptor.getContext();
String fullFileName = null;
FileInputStream fis = null;
try {
File file = attDescription.getMediaFile();
fullFileName = file.getAbsolutePath();
fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
GeoJsonParser parser = new GeoJsonParser();
List<GeoJsonFeature> features = parser.parse(reader);
fis.close();
fis = null;
logger.debug("Number of uploaded features: "+features.size());
int count = 0;
try {
for(GeoJsonFeature feature : features){
++count;
logger.debug("Creating geojson feature: "+count);
uploadFeature(feature, approvedContext);
}
} catch(Exception e) {
throw new Exception("Error while uploading GeoJSON features",e);
}
logger.debug("Done creating geojson features");
JSONObject doc = approvedContext.getDoc();
logger.debug("Obtained document");
// Create layer definition
{
JSONObject layerDef = new JSONObject();
layerDef.put("nunaliit_type", "layerDefinition");
layerDef.put("id", docDescriptor.getDocId());
String originalFileName = attDescription.getOriginalName();
if( null != originalFileName ) {
layerDef.put("name", "GeoJSON - "+originalFileName);
} else {
layerDef.put("name", "GeoJSON");
}
logger.debug("Computing BBOX");
// BBOX
{
boolean include = false;
BoundingBox boundingBox = new BoundingBox();
for(GeoJsonFeature feature : features){
Geometry geometry = feature.getGeometry();
if( null != geometry ){
geometry.extendBoundingBox(boundingBox);
include = true;
}
}
if( include ){
JSONArray bbox = new JSONArray();
bbox.put( boundingBox.getMinX() );
bbox.put( boundingBox.getMinY() );
bbox.put( boundingBox.getMaxX() );
bbox.put( boundingBox.getMaxY() );
layerDef.put("bbox", bbox);
}
}
doc.put("nunaliit_layer_definition", layerDef);
}
logger.debug("Updating layer definition document: "+doc.getString("_id"));
// Save changes to document
approvedContext.saveDocument();
logger.debug("Uploading file: "+attDescription.getAttachmentName());
// Upload original file
approvedContext.uploadFile(
attDescription.getAttachmentName()
,attDescription.getMediaFile()
,"application/xml"
);