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 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(); static final String SHPOUTPATH = "shpout"; private String dataOut = null; private HashMap featuresContext = new HashMap(); private HashMap featuresWriterContext = new HashMap(); private PessimisticMapWrapper txFeaturesContext; private FeatureTypeBuilder typeBuilder = null; private FeatureType featureType = 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); } 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() + "\\" + featureType.getTypeName()); logger.debug("Begin Save shapefile:" + sfile.toURI()); FeatureWriter writer; if(featuresWriterContext.containsKey(featureType.getTypeName())) { writer = (FeatureWriter) featuresWriterContext.get(featureType.getTypeName()) ; } else { ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL()); shapefileDataStore.createSchema(featureType); writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT); if(this.featuresWriterContext == null) { this.featuresWriterContext = new HashMap(); } featuresWriterContext.put(featureType.getTypeName() , writer); } ArrayList features = (ArrayList) featuresContext.get(featureType); Iterator itFeature = features.iterator(); while (itFeature.hasNext()) { Feature feature = (Feature) itFeature.next(); ((SimpleFeature) writer.next()).setAttributes(feature.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 = getDataPath() + SHPOUTPATH; } return dataOut; } 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 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())), }); Feature feature = featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); 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); } 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; } }