package com.ximple.eofms.filter; import com.vividsolutions.jts.geom.*; import com.ximple.eofms.util.*; import com.ximple.io.dgn7.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.geotools.feature.IllegalAttributeException; import org.geotools.feature.SchemaException; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import javax.swing.event.EventListenerList; import java.util.List; import java.util.TreeMap; public class CreateLineTextStrategy implements CreateFeatureTypeStrategy { static final Log logger = LogFactory.getLog(CreateLineTextStrategy.class); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); TreeMap typeBuilders = new TreeMap(); // Create the listener list protected EventListenerList listenerList = new EventListenerList(); public CreateLineTextStrategy() { } 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 SimpleFeatureType createFeatureElement(String featureName) throws SchemaException { if (!typeBuilders.containsKey(featureName)) { SimpleFeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createLineFeatureTypeBuilder(featureName); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); typeBuilders.put(featureName, featureType); fireFeatureTypeEvent(new FeatureTypeEvent(this, featureType)); } return typeBuilders.get(featureName); } public SimpleFeature createFeature(SimpleFeatureType featureType, Element element, boolean useTransform) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); FrammeAttributeData fLinkage = getFeatureLinkage(element); SimpleFeature feature = null; if (fLinkage == null) return null; if (element instanceof LineStringElement) { LineStringElement lineStringElement = (LineStringElement) element; Geometry gobj; if (useTransform) { GeometryConverterDecorator convertDecorator = FeatureTypeBuilderUtil.lookupDefaultGeometryConverter(); convertDecorator.setConverter(lineStringElement); gobj = convertDecorator.toGeometry(geometryFactory); } else { gobj = lineStringElement.toGeometry(geometryFactory); } if (gobj != null) gobj.setSRID(FeatureTypeBuilderUtil.getDefaultFeatureSRID()); feature = SimpleFeatureBuilder.build(featureType, 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() }, null); } else if (element instanceof TextElement) { TextElement txtElement = (TextElement) element; Coordinate ptOrigin = txtElement.getUserOrigin(); Coordinate ptEnd = new Coordinate(); ptEnd.x = ptOrigin.x; ptEnd.y = ptOrigin.y + txtElement.getTextHeight(); Coordinate[] vect = new Coordinate[2]; if (useTransform) { vect[0] = (FeatureTypeBuilderUtil.getDefaultFeatureSRID() == 3826) ? TWDDatumConverter.fromTM2ToEPSG3826(ptOrigin) : TWDDatumConverter.fromTM2ToEPSG3825(ptOrigin); vect[1] = FeatureTypeBuilderUtil.getDefaultFeatureSRID() == 3826 ? TWDDatumConverter.fromTM2ToEPSG3826(ptEnd) : TWDDatumConverter.fromTM2ToEPSG3825(ptEnd); } else { vect[0] = new Coordinate(ptOrigin); vect[1] = new Coordinate(ptEnd); } LineString line = geometryFactory.createLineString(vect); // convertDecorator.setConverter(txtElement); // Geometry geom = convertDecorator.toGeometry(geometryFactory); txtElement.getRotationAngle(); line.setSRID(FeatureTypeBuilderUtil.getDefaultFeatureSRID()); feature = SimpleFeatureBuilder.build(featureType, new Object[]{ line, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), fLinkage.getOccID(), (short) txtElement.getLevelIndex(), colorTable.getColorCode(txtElement.getColorIndex()), (short) txtElement.getWeight(), (short) txtElement.getLineStyle() }, null); } else if (element instanceof ComplexChainElement) { ComplexChainElement complexChain = (ComplexChainElement) element; Geometry gobj; if (useTransform) { GeometryConverterDecorator convertDecorator = FeatureTypeBuilderUtil.lookupDefaultGeometryConverter(); convertDecorator.setConverter(complexChain); gobj = convertDecorator.toGeometry(geometryFactory); } else { gobj = complexChain.toGeometry(geometryFactory); } if ((gobj != null) && (gobj instanceof MultiLineString)) { MultiLineString mline = (MultiLineString) gobj; CoordinateList coordinateList = new CoordinateList(); if (mline.getNumGeometries() == 1) { for (int i = 0; i < mline.getNumGeometries(); i++) { coordinateList.add(mline.getGeometryN(i).getCoordinates(), true); } } gobj = geometryFactory.createLineString(coordinateList.toCoordinateArray()); } if (gobj != null) gobj.setSRID(FeatureTypeBuilderUtil.getDefaultFeatureSRID()); feature = SimpleFeatureBuilder.build(featureType, 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() }, null); } else if (element instanceof LineElement) { LineElement lineElement = (LineElement) element; Geometry gobj; if (useTransform) { GeometryConverterDecorator convertDecorator = FeatureTypeBuilderUtil.lookupDefaultGeometryConverter(); convertDecorator.setConverter(lineElement); gobj = convertDecorator.toGeometry(geometryFactory); } else { gobj = lineElement.toGeometry(geometryFactory); } gobj.setSRID(FeatureTypeBuilderUtil.getDefaultFeatureSRID()); feature = SimpleFeatureBuilder.build(featureType, 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() }, null); } 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); } } } }