package com.ximple.eofms.filter; import javax.swing.event.EventListenerList; import java.util.List; import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.geotools.feature.Feature; import org.geotools.feature.FeatureType; import org.geotools.feature.FeatureTypeBuilder; import org.geotools.feature.IllegalAttributeException; import org.geotools.feature.SchemaException; import com.vividsolutions.jts.geom.CoordinateList; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.MultiLineString; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.EPSG3825GeometryConverterDecorator; import com.ximple.eofms.util.EPSG3826GeometryConverterDecorator; import com.ximple.eofms.util.FeatureTypeBuilderUtil; import com.ximple.eofms.util.GeometryConverterDecorator; import com.ximple.io.dgn7.ArcElement; import com.ximple.io.dgn7.ComplexChainElement; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.LineElement; import com.ximple.io.dgn7.LineStringElement; import com.ximple.io.dgn7.UserAttributeData; public class CreateLineStringStrategy implements CreateFeatureTypeStrategy { static final Log logger = LogFactory.getLog(CreateLineStringStrategy.class); GeometryFactory geometryFactory = new GeometryFactory(); TreeMap typeBuilders = new TreeMap(); static final GeometryConverterDecorator convertDecorator[] = new GeometryConverterDecorator[]{ new EPSG3826GeometryConverterDecorator(), new EPSG3825GeometryConverterDecorator() }; // Create the listener list protected EventListenerList listenerList = new EventListenerList(); public CreateLineStringStrategy() { } protected FrammeAttributeData getFeatureLinkage(Element element) { if (!element.hasUserAttributeData()) return null; List usrDatas = element.getUserAttributeData(); for (UserAttributeData anUsrData : usrDatas) { if (anUsrData instanceof FrammeAttributeData) { return (FrammeAttributeData) anUsrData; } } return null; } public FeatureType createFeatureElement(String featureName) throws SchemaException { if (!typeBuilders.containsKey(featureName)) { FeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createLineFeatureTypeBuilder(featureName); typeBuilders.put(featureName, typeBuilder); fireFeatureTypeEvent(new FeatureTypeEvent(this, typeBuilder.getFeatureType())); } return typeBuilders.get(featureName).getFeatureType(); } public Feature createFeature(FeatureType featureType, Element element, boolean useTransform, boolean useEPSG3826) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); FrammeAttributeData fLinkage = getFeatureLinkage(element); Feature feature = null; if (fLinkage == null) return null; if (element instanceof LineStringElement) { LineStringElement lineStringElement = (LineStringElement) element; Geometry gobj; if (useTransform) { if (useEPSG3826) { convertDecorator[0].setConverter(lineStringElement); gobj = convertDecorator[0].toGeometry(geometryFactory); } else { convertDecorator[1].setConverter(lineStringElement); gobj = convertDecorator[1].toGeometry(geometryFactory); } } else { gobj = lineStringElement.toGeometry(geometryFactory); } if (gobj != null) feature = featureType.create(new Object[]{ gobj, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), fLinkage.getOccID(), (short) lineStringElement.getLevelIndex(), colorTable.getColorCode(lineStringElement.getColorIndex()), (short) lineStringElement.getWeight(), (short) lineStringElement.getLineStyle() }); } else if (element instanceof ComplexChainElement) { ComplexChainElement complexChain = (ComplexChainElement) element; Geometry gobj; if (useTransform) { if (useEPSG3826) { convertDecorator[0].setConverter(complexChain); gobj = convertDecorator[0].toGeometry(geometryFactory); } else { convertDecorator[1].setConverter(complexChain); gobj = convertDecorator[1].toGeometry(geometryFactory); } } else { gobj = complexChain.toGeometry(geometryFactory); } if ((gobj != null) && (gobj instanceof MultiLineString)) { MultiLineString mline = (MultiLineString) gobj; CoordinateList coordinateList = new CoordinateList(); for (int i = 0; i < mline.getNumGeometries(); i++) { coordinateList.add(mline.getGeometryN(i).getCoordinates(), true); } gobj = geometryFactory.createLineString(coordinateList.toCoordinateArray()); } if (gobj != null) feature = featureType.create(new Object[]{ gobj, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), fLinkage.getOccID(), (short) complexChain.getLevelIndex(), colorTable.getColorCode(complexChain.getColorIndex()), (short) complexChain.getWeight(), (short) complexChain.getLineStyle() }); } else if (element instanceof LineElement) { LineElement lineElement = (LineElement) element; Geometry gobj; if (useTransform) { if (useEPSG3826) { convertDecorator[0].setConverter(lineElement); gobj = convertDecorator[0].toGeometry(geometryFactory); } else { convertDecorator[1].setConverter(lineElement); gobj = convertDecorator[1].toGeometry(geometryFactory); } } else { gobj = lineElement.toGeometry(geometryFactory); } if (gobj != null) feature = featureType.create(new Object[]{ gobj, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), fLinkage.getOccID(), (short) lineElement.getLevelIndex(), colorTable.getColorCode(lineElement.getColorIndex()), (short) lineElement.getWeight(), (short) lineElement.getLineStyle() }); return feature; } else if (element instanceof ArcElement) { ArcElement arcElement = (ArcElement) element; Geometry gobj; if (useTransform) { if (useEPSG3826) { convertDecorator[0].setConverter(arcElement); gobj = convertDecorator[0].toGeometry(geometryFactory); } else { convertDecorator[1].setConverter(arcElement); gobj = convertDecorator[1].toGeometry(geometryFactory); } } else { gobj = arcElement.toGeometry(geometryFactory); } if (gobj != null) feature = featureType.create(new Object[]{ gobj, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), fLinkage.getOccID(), (short) arcElement.getLevelIndex(), colorTable.getColorCode(arcElement.getColorIndex()), (short) arcElement.getWeight(), (short) arcElement.getLineStyle() }); } return feature; } public void addCreateFeatureTypeEventListener(CreateFeatureTypeEventListener listener) { listenerList.add(CreateFeatureTypeEventListener.class, listener); } public void removeCreateFeatureTypeEventListener(CreateFeatureTypeEventListener listener) { listenerList.remove(CreateFeatureTypeEventListener.class, listener); } protected void fireFeatureTypeEvent(FeatureTypeEvent evt) { Object[] listeners = listenerList.getListenerList(); for (int i = 0; i < listeners.length; i += 2) { if (listeners[i] == CreateFeatureTypeEventListener.class) { ((CreateFeatureTypeEventListener) listeners[i + 1]).createFeatureTypeOccurred(evt); } } } }