package com.ximple.eofms.jobs; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.charset.Charset; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.transaction.memory.PessimisticMapWrapper; import org.apache.commons.transaction.util.CommonsLoggingLogger; import org.apache.commons.transaction.util.LoggerFacade; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; 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 org.geotools.feature.SimpleFeature; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.TPCLIDConverter; import com.ximple.eofms.util.TWDDatumConverter; 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 IndexDgnConvertJobContext extends AbstractDgnFileJobContext { static final Log logger = LogFactory.getLog(IndexDgnConvertJobContext.class); static final LoggerFacade sLogger = new CommonsLoggingLogger(logger); static final GeometryFactory geometryFactory = new GeometryFactory(); TWD97GeometryConverterDecorator convertDecorator = new TWD97GeometryConverterDecorator(); static final String SHPOUTPATH = "shpout"; private String dataOut = null; private HashMap > featuresContext = new HashMap >(); private HashMap featuresWriterContext = new HashMap(); private PessimisticMapWrapper txFeaturesContext; private FeatureTypeBuilder typeBuilder2 = null; private FeatureTypeBuilder typeBuilder = null; private FeatureType featureType = null; private FeatureType featureType2 = null; public IndexDgnConvertJobContext(String dataPath) { super(dataPath); txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger); } public void putFeatureCollection(Element element) throws IllegalAttributeException, SchemaException { if (!(element instanceof TextElement)) { return; } Feature feature = createFeature((TextElement) element); if (feature == null) { logger.info("cannot craete feature." + element.toString() + "'" + ((TextElement) element).getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } ArrayList arrayList = (ArrayList) txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); feature = createFeature2((TextElement) element); if (feature == null) { logger.info("cannot craete feature2." + element.toString() + "'" + ((TextElement) element).getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } arrayList = (ArrayList) txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); } public void startTransaction() { } public void commitTransaction() { if (!txFeaturesContext.isEmpty()) { logger.debug("Transaction size = " + txFeaturesContext.size()); //txFeaturesContext.commitTransaction(); } else { logger.debug("Transaction is empty."); } if (!featuresContext.isEmpty()) { updateDataStore(); } } public void rollbackTransaction() { //txFeaturesContext.rollbackTransaction(); if (!featuresContext.isEmpty()) { updateDataStore(); } } private void updateDataStore() { Iterator it = featuresContext.keySet().iterator(); try { while (it.hasNext()) { FeatureType featureType = (FeatureType) it.next(); File sfile = new File(getDataOutPath() + File.separator + featureType.getTypeName()); logger.debug("Begin Save shapefile:" + sfile.toURI()); FeatureWriter writer; if(featuresWriterContext.containsKey(featureType.getTypeName())) { writer = featuresWriterContext.get(featureType.getTypeName()) ; } else { ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(), true, Charset.forName("UTF-8")); shapefileDataStore.createSchema(featureType); writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT); featuresWriterContext.put(featureType.getTypeName() , writer); } ArrayList features = featuresContext.get(featureType); for (Feature feature1 : features) { ((SimpleFeature) writer.next()).setAttributes(feature1.getAttributes(null)); } //writer.close(); logger.debug("End Save shapefile:" + sfile.toURI()); } featuresContext.clear(); } catch (MalformedURLException e) { logger.error(e.getMessage(), e); } catch (IllegalAttributeException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } } public String getDataOutPath() { if (dataOut == null) { File outPath = new File(getDataPath(), SHPOUTPATH); if (!outPath.exists()) { outPath.mkdir(); } else if (!outPath.isDirectory()) { outPath.mkdir(); } dataOut = outPath.toString(); } return dataOut; } public void closeFeatureWriter() throws IOException { for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { featureWriter.close(); } this.featuresWriterContext.clear(); } public FeatureType createFeatureElement(String featureName) throws SchemaException { if (typeBuilder == null) { typeBuilder = FeatureTypeBuilder.newInstance(featureName); typeBuilder.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("X1", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("Y1", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("X2", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("Y2", Double.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("TPCID", String.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", String.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMWEIGHT", Integer.class)); typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMSTYLE", Integer.class)); } return typeBuilder.getFeatureType(); } public FeatureType createFeatureElement2(String featureName) throws SchemaException { if (typeBuilder2 == null) { typeBuilder2 = FeatureTypeBuilder.newInstance(featureName); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", String.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("SYMWEIGHT", Integer.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("SYMSTYLE", Integer.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("JUST", Integer.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("HEIGHT", Double.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("WIDTH", Double.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("ANGLE", Double.class)); typeBuilder2.addType(AttributeTypeFactory.newAttributeType("TPCID", String.class)); } return typeBuilder2.getFeatureType(); } public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { TextElement textElement = (TextElement) element; String tpclid = textElement.getText(); Envelope extent = TPCLIDConverter.convertTpclIdToEnvelope(tpclid); Geometry geom = geometryFactory.createLinearRing(new Coordinate[] { TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMinY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMaxX(), extent.getMinY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMaxX(), extent.getMaxY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMaxY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMinY())), }); return featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); } return null; } public Feature createFeature2(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { TextElement txtElement = (TextElement) element; double angle = txtElement.getRotationAngle(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); convertDecorator.setConverter(txtElement); Feature feature = featureType.create(new Object[]{ convertDecorator.toGeometry(geometryFactory), colorTable.getColorCode(txtElement.getColorIndex()), txtElement.getWeight(), txtElement.getLineStyle(), txtElement.getJustification(), txtElement.getTextHeight(), txtElement.getTextWidth(), angle, txtElement.getText() }); return feature; } return null; } private Feature createFeature(TextElement element) throws SchemaException, IllegalAttributeException { if (featureType == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } featureType = createFeatureElement(dgnname); } return createFeature(featureType, element); } private Feature createFeature2(TextElement element) throws SchemaException, IllegalAttributeException { if (featureType2 == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } dgnname = dgnname + "P"; featureType2 = createFeatureElement2(dgnname); } return createFeature2(featureType2, element); } 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; } }