package com.ximple.eofms.jobs.context.postgis; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; 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.util.CommonsLoggingLogger; import org.apache.commons.transaction.util.LoggerFacade; import org.geotools.data.DataStore; import org.geotools.data.Transaction; 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.postgresql.util.PSQLException; 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.vividsolutions.jts.geom.LinearRing; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.FeatureTypeBuilderUtil; import com.ximple.eofms.util.TPCLIDConverter; import com.ximple.eofms.util.TWD97GeometryConverterDecorator; import com.ximple.eofms.util.TWDDatumConverter; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.ShapeElement; import com.ximple.io.dgn7.TextElement; import com.ximple.io.dgn7.UserAttributeData; public class IndexDgnConvertPostGISJobContext extends AbstractDgnToPostGISJobContext { static final Log logger = LogFactory.getLog(IndexDgnConvertPostGISJobContext.class); static final LoggerFacade sLogger = new CommonsLoggingLogger(logger); static final GeometryFactory geometryFactory = new GeometryFactory(); TWD97GeometryConverterDecorator convertDecorator = new TWD97GeometryConverterDecorator(); private HashMap> txFeaturesContext = new HashMap>(); private FeatureTypeBuilder typeBuilderPnt = null; private FeatureTypeBuilder typeBuilderRect = null; private FeatureTypeBuilder typeBuilderSmallRect = null; private FeatureType featureType = null; private FeatureType featureType2 = null; private FeatureType featureType3 = null; private boolean dropTableMode = true; private int accumulate = 0; public IndexDgnConvertPostGISJobContext(String dataPath, DataStore targetDataStore, String targetSchema) { super(dataPath, targetDataStore, targetSchema); } public void putFeatureCollection(Element element) throws IllegalAttributeException, SchemaException { if ((!(element instanceof TextElement)) && (!(element instanceof ShapeElement))) { return; } if ((element instanceof TextElement)) { putTextFeatureCollection((TextElement) element); } if ((element instanceof ShapeElement)) { putShapeFeatureCollection((ShapeElement) element); } } protected void putTextFeatureCollection(TextElement element) throws SchemaException, IllegalAttributeException { Feature feature = createFeature(element); if (feature == null) { logger.info("cannot craete feature." + element.toString() + "'" + element.getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } ArrayList arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); feature = createFeature2(element); if (feature == null) { logger.info("cannot craete feature2." + element.toString() + "'" + element.getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); Feature[] features = createFeature3(element); if (features == null) { logger.info("cannot craete feature3." + element.toString() + "'" + element.getText() + "'"); return; } if (!txFeaturesContext.containsKey(features[0].getFeatureType())) { txFeaturesContext.put(features[0].getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(features[0].getFeatureType()); arrayList.addAll(Arrays.asList(features)); accumulate++; if (accumulate > BATCHSIZE) { commitTransaction(); } } protected void putShapeFeatureCollection(ShapeElement element) throws SchemaException, IllegalAttributeException { Feature feature = createFeature(element); if (feature == null) { LinearRing ring = (LinearRing) element.toGeometry(geometryFactory); if (ring == null) { logger.info("cannot craete feature." + element.toString() + "'" + "linear is null" + "'"); } else { Coordinate pt = ring.getEnvelopeInternal().centre(); String id = TPCLIDConverter.CoordinateToTpclId(pt); logger.info("cannot craete feature." + element.toString() + "'" + id + "'- from pt=" + pt); } return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } ArrayList arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); feature = createFeature2(element); if (feature == null) { LinearRing ring = (LinearRing) element.toGeometry(geometryFactory); if (ring == null) { logger.info("cannot craete feature2." + element.toString() + "'" + "linear is null" + "'"); } else { Coordinate pt = ring.getEnvelopeInternal().centre(); String id = TPCLIDConverter.CoordinateToTpclId(pt); logger.info("cannot craete feature2." + element.toString() + "'" + id + "'- from pt=" + pt); } return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); Feature[] features = createFeature3(element); if (features == null) { LinearRing ring = (LinearRing) element.toGeometry(geometryFactory); if (ring == null) { logger.info("cannot craete feature3." + element.toString() + "'" + "linear is null" + "'"); } else { Coordinate pt = ring.getEnvelopeInternal().centre(); String id = TPCLIDConverter.CoordinateToTpclId(pt); logger.info("cannot craete feature3." + element.toString() + "'" + id + "'- from pt=" + pt); } return; } if (!txFeaturesContext.containsKey(features[0].getFeatureType())) { txFeaturesContext.put(features[0].getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(features[0].getFeatureType()); arrayList.addAll(Arrays.asList(features)); accumulate++; if (accumulate > BATCHSIZE) { commitTransaction(); } } public void startTransaction() { } public void commitTransaction() { if (!txFeaturesContext.isEmpty()) { logger.debug("Transaction size = " + txFeaturesContext.size()); } else { logger.debug("Transaction is empty."); } if (!txFeaturesContext.isEmpty()) { updateDataStore(); } } public void rollbackTransaction() { } private void updateDataStore() { Iterator it = txFeaturesContext.keySet().iterator(); try { while (it.hasNext()) { FeatureType featureType = it.next(); logger.debug("Begin Save PostGIS:" + featureType.getTypeName()); String bindingStmt = makePrepareInsertSql(featureType); ArrayList features = txFeaturesContext.get(featureType); Connection conn = getConnection(); boolean autoCommit = conn.getAutoCommit(); conn.setAutoCommit(true); PreparedStatement pstmt = conn.prepareStatement(bindingStmt); for (Feature feature : features) { // currentStmt = feature; // Statement stmt = conn.createStatement(); try { // stmt.execute(feature); bindFeatureParameters(pstmt, feature); pstmt.execute(); } catch (PSQLException e) { if (bindingStmt != null) { logger.error("Execute:" + bindingStmt); } logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); /* } finally { stmt.close(); */ } } /* if ((i % BATCHSIZE) != 0) { stmt.executeBatch(); } stmt.close(); */ pstmt.close(); features.clear(); conn.setAutoCommit(autoCommit); logger.debug("End Save PostGIS:" + featureType.getTypeName()); } accumulate = 0; } catch (PSQLException e) { logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); } catch (SQLException e) { logger.error(e.getMessage(), e); } } public void closeFeatureWriter() throws IOException { txFeaturesContext.clear(); /* for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { featureWriter.close(); } this.featuresWriterContext.clear(); */ } public FeatureType createFeatureElement(String featureName) throws SchemaException { if (typeBuilderRect == null) { typeBuilderRect = FeatureTypeBuilderUtil.createNormalIndexFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderRect.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { try { dropGeometryColumn(conn, featureName, typeBuilderRect.getFeatureType().getDefaultGeometry().getLocalName()); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } try { dropTable(conn, featureName); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } ArrayList schemaTexts = createNewSchemaTexts(typeBuilderRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderRect.getFeatureType(); } public FeatureType createFeatureElement2(String featureName) throws SchemaException { if (typeBuilderPnt == null) { typeBuilderPnt = FeatureTypeBuilderUtil.createNormalIndexTextFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderPnt.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { dropGeometryColumn(conn, featureName, typeBuilderPnt.getFeatureType().getDefaultGeometry().getLocalName()); dropTable(conn, featureName); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderPnt.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderPnt.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderPnt.getFeatureType(); } public FeatureType createFeatureElement3(String featureName) throws SchemaException { if (typeBuilderSmallRect == null) { typeBuilderSmallRect = FeatureTypeBuilderUtil.createNormalIndexFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderSmallRect.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { try { dropGeometryColumn(conn, featureName, typeBuilderSmallRect.getFeatureType().getDefaultGeometry().getLocalName()); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } try { dropTable(conn, featureName); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } ArrayList schemaTexts = createNewSchemaTexts(typeBuilderSmallRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderSmallRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderSmallRect.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.createPolygon(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())), }), null); return featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); } else if (element instanceof ShapeElement) { ShapeElement shapeElement = (ShapeElement) element; Geometry geomShape = shapeElement.toGeometry(geometryFactory); LinearRing linearRing = (LinearRing) geomShape; if (linearRing.isRectangle()) { Envelope bounds = linearRing.getEnvelopeInternal(); if (bounds.getWidth() == TPCLIDConverter.SX1200) { Coordinate center = bounds.centre(); String tpclid = TPCLIDConverter.CoordinateToTpclId(center); if (tpclid.length() > 5) { tpclid = tpclid.substring(0, 4); } Envelope extent = TPCLIDConverter.convertTpclIdToEnvelope(tpclid); Geometry geom = geometryFactory.createPolygon(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())), }), null); return featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid, colorTable.getColorCode(shapeElement.getColorIndex()), shapeElement.getWeight(), shapeElement.getLineStyle() }); } } } return null; } public Feature createFeature2(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { Feature feature = null; TextElement txtElement = (TextElement) element; double angle = txtElement.getRotationAngle(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); convertDecorator.setConverter(txtElement); Geometry gobj = convertDecorator.toGeometry(geometryFactory); if (gobj != null) feature = featureType.create(new Object[]{ gobj, colorTable.getColorCode(txtElement.getColorIndex()), txtElement.getWeight(), txtElement.getLineStyle(), txtElement.getJustification(), txtElement.getTextHeight(), txtElement.getTextWidth(), angle, txtElement.getText() }); return feature; } else if (element instanceof ShapeElement) { Feature feature = null; ShapeElement shapeElement = (ShapeElement) element; double angle = 0.0; Geometry geomShape = shapeElement.toGeometry(geometryFactory); LinearRing linearRing = (LinearRing) geomShape; if (linearRing.isRectangle()) { Envelope bounds = linearRing.getEnvelopeInternal(); if (bounds.getWidth() == TPCLIDConverter.SX1200) { Coordinate center = bounds.centre(); String tpclid = TPCLIDConverter.CoordinateToTpclId(center); if (tpclid.length() > 5) { tpclid = tpclid.substring(0, 4); Coordinate pos = TWDDatumConverter.fromTM2ToTWD97(new Coordinate(center.x, center.y)); Geometry gobj = geometryFactory.createPoint(pos); if (gobj != null) feature = featureType.create(new Object[]{ gobj, colorTable.getColorCode(shapeElement.getColorIndex()), shapeElement.getWeight(), shapeElement.getLineStyle(), 0, 15.0, 15 * 5, angle, tpclid }); return feature; } } } } return null; } public Feature[] createFeature3(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { TextElement textElement = (TextElement) element; String tpclid = textElement.getText(); Feature[] result = new Feature[4]; Envelope extent = TPCLIDConverter.convertTpclIdToEnvelope(tpclid); for (int i = 0; i < 4; i++) { char mapSubId = TPCLIDConverter.intToAscii(65 + i); int dx = (i % 2) * TPCLIDConverter.SX600; int dy = (i / 2) * TPCLIDConverter.SY600; Geometry geom = geometryFactory.createPolygon(geometryFactory.createLinearRing(new Coordinate[] { TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), }), null); Envelope innerExtent = geom.getEnvelopeInternal(); result[i] = featureType.create(new Object[]{ geom, innerExtent.getMinX(), innerExtent.getMinY(), innerExtent.getMaxX(), innerExtent.getMaxY(), tpclid + mapSubId, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); } return result; } else if (element instanceof ShapeElement) { ShapeElement shapeElement = (ShapeElement) element; Geometry geomShape = shapeElement.toGeometry(geometryFactory); LinearRing linearRing = (LinearRing) geomShape; if (linearRing.isRectangle()) { Envelope extent = linearRing.getEnvelopeInternal(); if (extent.getWidth() == TPCLIDConverter.SX1200) { Feature[] result = new Feature[4]; Coordinate center = extent.centre(); String tpclid = TPCLIDConverter.CoordinateToTpclId(center); for (int i = 0; i < 4; i++) { char mapSubId = TPCLIDConverter.intToAscii(65 + i); int dx = (i % 2) * TPCLIDConverter.SX600; int dy = (i / 2) * TPCLIDConverter.SY600; Geometry geom = geometryFactory.createPolygon(geometryFactory.createLinearRing(new Coordinate[] { TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), }), null); Envelope innerExtent = geom.getEnvelopeInternal(); result[i] = featureType.create(new Object[]{ geom, innerExtent.getMinX(), innerExtent.getMinY(), innerExtent.getMaxX(), innerExtent.getMaxY(), tpclid + mapSubId, colorTable.getColorCode(shapeElement.getColorIndex()), shapeElement.getWeight(), shapeElement.getLineStyle() }); } return result; } } } return null; } private Feature createFeature(Element 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.toLowerCase()); } return createFeature(featureType, element); } private Feature createFeature2(Element 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.toLowerCase()); } return createFeature2(featureType2, element); } private Feature[] createFeature3(Element element) throws SchemaException, IllegalAttributeException { if (featureType3 == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } dgnname = dgnname + "_s"; featureType3 = createFeatureElement3(dgnname.toLowerCase()); } return createFeature3(featureType3, 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; } public Log getLogger() { return logger; } public boolean isDropTableMode() { return dropTableMode; } public void setDropTableMode(boolean dropTableMode) { this.dropTableMode = dropTableMode; } public void clearOutputDatabase() { } }