package com.ximple.eofms.filter; import java.util.List; import java.util.TreeMap; import java.math.BigDecimal; import java.math.RoundingMode; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.geotools.feature.AttributeTypeFactory; 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.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.ximple.eofms.jobs.TWD97GeometryConverterDecorator; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.TextElement; import com.ximple.io.dgn7.UserAttributeData; public class CreateSymbolStrategy implements CreateFeatureTypeStrategy { static final Log logger = LogFactory.getLog(CreateSymbolStrategy.class); GeometryFactory geometryFactory = new GeometryFactory(); TreeMap typeBuilders = new TreeMap(); TWD97GeometryConverterDecorator convertDecorator = new TWD97GeometryConverterDecorator(); public CreateSymbolStrategy() { } 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 = FeatureTypeBuilder.newInstance(featureName); typeBuilder.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("TID", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("OID", Long.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("CID", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("LID", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("LEVEL", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", String.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMWEIGHT", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMSTYLE", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("JUST", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("HEIGHT", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("WIDTH", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("ANGLE", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMBOL", String.class)); typeBuilders.put(featureName, typeBuilder); } return typeBuilders.get(featureName).getFeatureType(); } public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); FrammeAttributeData fLinkage = getFeatureLinkage(element); if (fLinkage == null) return null; if (element instanceof TextElement) { TextElement txtElement = (TextElement) element; double angle = txtElement.getRotationAngle(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); if (txtElement.getText().length() == 0) { logger.info("CreateSymbolStrategy cannot conver " + element.toString() + "to Feature - getText() is empty."); return null; } StringBuilder sb = new StringBuilder(); sb.append("OCT"); char id = txtElement.getText().toCharArray()[0]; sb.append(Integer.toOctalString((int) id)); sb.append("-"); sb.append(txtElement.getFontIndex()); convertDecorator.setConverter(txtElement); Feature feature = featureType.create(new Object[]{ convertDecorator.toGeometry(geometryFactory), (int) fLinkage.getFsc(), (long) fLinkage.getUfid(), (int) fLinkage.getComponentID(), 0, txtElement.getLevelIndex(), colorTable.getColorCode(txtElement.getColorIndex()), txtElement.getWeight(), txtElement.getLineStyle(), txtElement.getJustification(), txtElement.getTextHeight(), txtElement.getTextWidth(), angle, sb.toString() }); return feature; } else { logger.info("CreateSymbolStrategy cannot conver " + element.toString() + "to Feature"); } return null; } }