/*
* This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
*
* Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
*
* The program is available in open source according to the GNU Affero
* General Public License. All contributions in this program are covered
* by the Geomajas Contributors License Agreement. For full licensing
* details, see LICENSE.txt in the project root.
*/
package org.geomajas.rest.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.geomajas.configuration.AttributeInfo;
import org.geomajas.configuration.PrimitiveAttributeInfo;
import org.geomajas.configuration.VectorLayerInfo;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.InternalFeature;
import org.geomajas.service.GeoService;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.vividsolutions.jts.geom.Geometry;
/**
* Service to convert from Geomajas to GeoTools objects.
*
* @author Oliver May
* @author Jan De Moerloose
*/
@Component
public class GeoToolsConverterServiceImpl implements GeoToolsConverterService {
private static final String NAMESPACE_URI = "http://www.geomajas.org";
private final Logger log = LoggerFactory.getLogger(GeoToolsConverterServiceImpl.class);
@Autowired
private GeoService geoservice;
/** {@inheritDoc} */
public SimpleFeatureType toSimpleFeatureType(VectorLayerInfo vectorLayerInfo) throws LayerException {
return toSimpleFeatureType(vectorLayerInfo, null);
}
/** {@inheritDoc} */
public SimpleFeature toSimpleFeature(InternalFeature feature, SimpleFeatureType featureType) {
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
List<Object> attr = new ArrayList<Object>();
for (AttributeDescriptor ads : featureType.getAttributeDescriptors()) {
if (!ads.equals(featureType.getGeometryDescriptor())) {
Attribute a = feature.getAttributes().get(ads.getName().getLocalPart());
if (null != a) {
attr.add(a.getValue());
} else {
attr.add(null);
}
} else {
attr.add(feature.getGeometry());
}
}
return builder.buildFeature(feature.getId(), attr.toArray());
}
/** {@inheritDoc} */
public SimpleFeatureType toSimpleFeatureType(VectorLayerInfo vectorLayerInfo, List<String> attributeNames)
throws LayerException {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(vectorLayerInfo.getFeatureInfo().getDataSourceName());
builder.setNamespaceURI(NAMESPACE_URI);
builder.setCRS(geoservice.getCrs2(vectorLayerInfo.getCrs()));
// create a lookup map of attribute info's
Map<String, AttributeInfo> attrs = new LinkedHashMap<String, AttributeInfo>();
for (AttributeInfo a : vectorLayerInfo.getFeatureInfo().getAttributes()) {
attrs.put(a.getName(), a);
}
if (attributeNames == null) {
attributeNames = new ArrayList<String>(attrs.keySet());
}
// now list 'm up
for (String name : attributeNames) {
if (attrs.containsKey(name)) {
AttributeInfo a = attrs.get(name);
if (a instanceof PrimitiveAttributeInfo) {
PrimitiveAttributeInfo attr = (PrimitiveAttributeInfo) a;
switch (attr.getType()) {
case BOOLEAN:
builder.add(attr.getName(), Boolean.class);
break;
case SHORT:
builder.add(attr.getName(), Short.class);
break;
case INTEGER:
builder.add(attr.getName(), Integer.class);
break;
case LONG:
builder.add(attr.getName(), Long.class);
break;
case FLOAT:
builder.add(attr.getName(), Float.class);
break;
case DOUBLE:
builder.add(attr.getName(), Double.class);
break;
case CURRENCY:
case STRING:
case URL:
case IMGURL:
builder.add(attr.getName(), String.class);
break;
case DATE:
builder.add(attr.getName(), Date.class);
break;
default:
log.error("Don't know how to convert attribute of type " + attr.getType() + ", skipped, " +
attr);
break;
}
}
}
}
builder.add(vectorLayerInfo.getFeatureInfo().getGeometryType().getName(), Geometry.class);
builder.setDefaultGeometry(vectorLayerInfo.getFeatureInfo().getGeometryType().getName());
return builder.buildFeatureType();
}
}