Date created = new Date();
CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
JSONObj proj = obj.object("proj");
if (proj != null) {
try {
crs = IO.crs(proj);
} catch (Exception e) {
throw new BadRequestException("Error parsing proj: " + proj.toString());
}
}
else {
throw new BadRequestException("Map object requires projection");
}
ReferencedEnvelope bounds = null;
boolean updateBounds = false;
if (obj.has("bbox")) {
Envelope envelope = IO.bounds(obj.object("bbox"));
bounds = new ReferencedEnvelope( envelope, crs );
}
else {
bounds = new ReferencedEnvelope(crs);
updateBounds = true;
}
if (!obj.has("layers")) {
throw new BadRequestException("Map object requires layers array");
}
LayerGroupInfo map = cat.getFactory().createLayerGroup();
map.setName( name );
map.setAbstract( description );
map.setTitle( title );
map.setMode( Mode.SINGLE );
map.setWorkspace(ws);
for (Object o : obj.array("layers")) {
JSONObj l = (JSONObj) o;
LayerInfo layer = findLayer(wsName, l.str("name"), cat);
map.getLayers().add(layer);
map.getStyles().add(null);
if (updateBounds) {
try {
updateBounds(bounds, layer);
} catch (Exception e) {
throw new RuntimeException("Error calculating map bounds ", e);
}
}
}
map.setBounds( bounds );
Metadata.created(map, created);
Metadata.modified(map, created);
Metadata.modified(ws, created);
cat.add( map );
cat.save(ws);
recent.add(LayerGroupInfo.class, map);
return mapDetails(new JSONObj(), map, wsName, req);
}