Commit 681c4240 authored by Syrus Mesdaghi's avatar Syrus Mesdaghi Committed by Andrea Aime
Browse files

[GEOS-7012] fixes GeoJSON import through rest api

parent 30c3a819
......@@ -20,6 +20,10 @@ import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.StoreInfo;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.CatalogFactory;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.AttributeTypeInfo;
import org.geotools.data.FeatureReader;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
......@@ -36,6 +40,7 @@ import org.geoserver.importer.job.ProgressMonitor;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.FeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
......@@ -46,6 +51,7 @@ import com.google.common.collect.Iterables;
public class GeoJSONFormat extends VectorFormat {
static Logger LOG = Logging.getLogger(GeoJSONFormat.class);
private static ReferencedEnvelope EMPTY_BOUNDS = new ReferencedEnvelope();
@Override
public FeatureReader read(ImportData data, ImportTask item) throws IOException {
......@@ -147,6 +153,8 @@ public class GeoJSONFormat extends VectorFormat {
ImportTask task(ImportData data, Catalog catalog) throws IOException {
File file = maybeFile(data).get();
CatalogFactory factory = catalog.getFactory();
CatalogBuilder catalogBuilder = new CatalogBuilder(catalog);
// get the composite feature type
SimpleFeatureType featureType = new FeatureJSON().readFeatureCollectionSchema(file, false);
......@@ -162,6 +170,14 @@ public class GeoJSONFormat extends VectorFormat {
ft.setName(FilenameUtils.getBaseName(file.getName()));
ft.setNativeName(ft.getName());
List<AttributeTypeInfo> attributes = ft.getAttributes();
for (AttributeDescriptor ad : featureType.getAttributeDescriptors()) {
AttributeTypeInfo att = factory.createAttribute();
att.setName(ad.getLocalName());
att.setBinding(ad.getType().getBinding());
attributes.add(att);
}
// crs
CoordinateReferenceSystem crs = null;
if (featureType != null && featureType.getCoordinateReferenceSystem() != null) {
......@@ -182,19 +198,11 @@ public class GeoJSONFormat extends VectorFormat {
}
// bounds
ReferencedEnvelope bounds = new ReferencedEnvelope(crs);
FeatureJSON reader = new FeatureJSON();
reader.setFeatureType(featureType);
FeatureIterator<SimpleFeature> it = reader.streamFeatureCollection(file);
while(it.hasNext()) {
SimpleFeature f = it.next();
bounds.include(f.getBounds());
}
ft.setNativeBoundingBox(bounds);
ft.setNativeBoundingBox(EMPTY_BOUNDS);
ft.setLatLonBoundingBox(EMPTY_BOUNDS);
ft.getMetadata().put("recalculate-bounds", Boolean.TRUE);
LayerInfo layer = catalog.getFactory().createLayer();
layer.setResource(ft);
LayerInfo layer = catalogBuilder.buildLayer((ResourceInfo) ft);
ImportTask task = new ImportTask(data);
task.setLayer(layer);
......
......@@ -6,6 +6,7 @@
package org.geoserver.importer.rest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -306,4 +307,33 @@ public class ImportResourceTest extends ImporterTestSupport {
res = dispatch(req);
assertEquals("text/html", res.getContentType());
}
@Test
public void testGetImportGeoJSON() throws Exception {
File dir = unpack("geojson/point.json.zip");
importer.createContext(new SpatialFile(new File(dir, "point.json")));
int id = lastId();
JSONObject json = (JSONObject) getAsJSON("/rest/imports/" + id + "?expand=3");
assertNotNull(json);
JSONObject imprt = json.optJSONObject("import");
assertEquals(id, imprt.getInt("id"));
JSONArray tasks = imprt.getJSONArray("tasks");
assertEquals(1, tasks.size());
JSONObject task = tasks.getJSONObject(0);
JSONObject source = task.getJSONObject("data");
assertEquals("file", source.getString("type"));
assertEquals("GeoJSON", source.getString("format"));
assertEquals("point.json", source.getString("file"));
JSONObject layer = task.getJSONObject("layer");
JSONArray attributes = layer.getJSONArray("attributes");
assertNotEquals(0, attributes.size());
assertTrue(layer.containsKey("bbox"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment