Search in sources :

Example 1 with IoxException

use of ch.interlis.iox.IoxException in project ili2db by claeis.

the class Fgdb2iox method readGeometry.

private IomObject readGeometry() throws IOException, ParseException, IoxException {
    dis.setOrder(ByteOrderValues.LITTLE_ENDIAN);
    int typeInt = dis.readInt();
    int geometryType = typeInt & EsriShpConstants.shapeBasicTypeMask;
    if (geometryType == EsriShpConstants.ShapeNull) {
        return null;
    }
    // determine if Z values are present
    hasZ = geometryType == EsriShpConstants.ShapePointZM || geometryType == EsriShpConstants.ShapePointZ || geometryType == EsriShpConstants.ShapeMultiPointZM || geometryType == EsriShpConstants.ShapeMultiPointZ || geometryType == EsriShpConstants.ShapePolylineZM || geometryType == EsriShpConstants.ShapePolylineZ || geometryType == EsriShpConstants.ShapePolygonZM || geometryType == EsriShpConstants.ShapePolygonZ || (geometryType == EsriShpConstants.ShapeGeneralPoint || geometryType == EsriShpConstants.ShapeGeneralPolyline || geometryType == EsriShpConstants.ShapeGeneralPolygon || geometryType == EsriShpConstants.ShapeGeneralMultiPoint || geometryType == EsriShpConstants.ShapeGeneralMultiPatch) && ((typeInt & EsriShpConstants.shapeHasZs) != 0);
    boolean hasM = geometryType == EsriShpConstants.ShapePointZM || geometryType == EsriShpConstants.ShapePointM || geometryType == EsriShpConstants.ShapeMultiPointZM || geometryType == EsriShpConstants.ShapeMultiPointM || geometryType == EsriShpConstants.ShapePolylineZM || geometryType == EsriShpConstants.ShapePolylineM || geometryType == EsriShpConstants.ShapePolygonZM || geometryType == EsriShpConstants.ShapePolygonM || (geometryType == EsriShpConstants.ShapeGeneralPoint || geometryType == EsriShpConstants.ShapeGeneralPolyline || geometryType == EsriShpConstants.ShapeGeneralPolygon || geometryType == EsriShpConstants.ShapeGeneralMultiPoint || geometryType == EsriShpConstants.ShapeGeneralMultiPatch) && ((typeInt & EsriShpConstants.shapeHasMs) != 0);
    boolean hasCurves = (geometryType == EsriShpConstants.ShapeGeneralPolyline || geometryType == EsriShpConstants.ShapeGeneralPolygon) && (typeInt & EsriShpConstants.shapeNonBasicModifierMask) != 0 || (typeInt & EsriShpConstants.shapeHasCurves) != 0;
    inputDimension = hasZ ? 3 : 2;
    // only allocate ordValues buffer if necessary
    if (ordValues == null || ordValues.length < inputDimension)
        ordValues = new double[inputDimension];
    if (geometryType == EsriShpConstants.ShapePoint || geometryType == EsriShpConstants.ShapePointZM || geometryType == EsriShpConstants.ShapePointZ || geometryType == EsriShpConstants.ShapeGeneralPoint) {
        double x = dis.readDouble();
        double y = dis.readDouble();
        IomObject ret = new ch.interlis.iom_j.Iom_jObject("COORD", null);
        ret.setattrvalue("C1", Double.toString(x));
        ret.setattrvalue("C2", Double.toString(y));
        if (hasZ) {
            double z = dis.readDouble();
            ret.setattrvalue("C3", Double.toString(z));
        }
        return ret;
    }
    if (geometryType == EsriShpConstants.ShapeGeneralMultiPatch) {
        throw new IoxException("unexpected geometryType " + geometryType);
    }
    // boundingBox
    double min_x = dis.readDouble();
    double min_y = dis.readDouble();
    double max_x = dis.readDouble();
    double max_y = dis.readDouble();
    // cParts
    int cParts = 0;
    int[] partStart = null;
    if (geometryType == EsriShpConstants.ShapeMultiPoint || geometryType == EsriShpConstants.ShapeMultiPointZM || geometryType == EsriShpConstants.ShapeMultiPointZ || geometryType == EsriShpConstants.ShapeGeneralMultiPoint) {
    } else {
        cParts = dis.readInt();
        partStart = new int[cParts];
    }
    // cPoints
    int cPoints = dis.readInt();
    // parts[cParts]
    if (cParts > 0) {
        for (int i = 0; i < cParts; i++) {
            partStart[i] = dis.readInt();
        }
    }
    // points[cPoints]
    Coordinate[] points = new Coordinate[cPoints];
    for (int i = 0; i < cPoints; i++) {
        points[i] = new Coordinate();
        points[i].x = dis.readDouble();
        points[i].y = dis.readDouble();
    }
    if (geometryType == EsriShpConstants.ShapeMultiPoint || geometryType == EsriShpConstants.ShapeMultiPointZM || geometryType == EsriShpConstants.ShapeMultiPointZ || geometryType == EsriShpConstants.ShapeGeneralMultiPoint) {
        throw new IoxException("unexpected geometryType " + geometryType);
    } else if (geometryType == EsriShpConstants.ShapePolyline || geometryType == EsriShpConstants.ShapePolylineZM || geometryType == EsriShpConstants.ShapePolylineZ || geometryType == EsriShpConstants.ShapeGeneralPolyline) {
    } else if (geometryType == EsriShpConstants.ShapePolygon || geometryType == EsriShpConstants.ShapePolygonZM || geometryType == EsriShpConstants.ShapePolygonZ || geometryType == EsriShpConstants.ShapeGeneralPolygon) {
    } else {
        throw new IoxException("unexpected geometryType " + geometryType);
    }
    if (hasZ) {
        double min_z = dis.readDouble();
        double max_z = dis.readDouble();
        // Zs[cPoints]
        for (int i = 0; i < cPoints; i++) {
            points[i].z = dis.readDouble();
        }
    }
    if (hasM) {
        double min_m = dis.readDouble();
        double max_m = dis.readDouble();
        // Ms[cPoints]
        for (int i = 0; i < cPoints; i++) {
            // ignore
            dis.readDouble();
        }
    }
    java.util.Map<Integer, Arc> arcs = null;
    if (hasCurves) {
        int cSegmentModifiers = dis.readInt();
        arcs = new java.util.HashMap<Integer, Arc>();
        for (int i = 0; i < cSegmentModifiers; i++) {
            int startPointIndex = dis.readInt();
            int segmentType = dis.readInt();
            if (segmentType == EsriShpConstants.segmentArc) {
                double v1 = dis.readDouble();
                double v2 = dis.readDouble();
                int bits = dis.readInt();
                // int skip1=dis.readInt();
                if ((bits & EsriShpConstants.arcIsEmpty) != 0) {
                // skip it
                } else if ((bits & EsriShpConstants.arcIsLine) != 0) {
                // straight line, skip it
                } else if ((bits & EsriShpConstants.arcIsPoint) != 0) {
                    throw new IoxException("not supported SegmentArc.Bits " + bits);
                } else if ((bits & EsriShpConstants.arcDefinedIP) != 0) {
                // throw new IoxException("not supported SegmentArc.Bits "+bits);
                } else {
                    if ((bits & EsriShpConstants.arcIsCCW) != 0) {
                    // counterclockwise
                    } else {
                    // clockwise
                    }
                }
                // double skip1=dis.readDouble();
                // double skip2=dis.readDouble();
                // double skip3=dis.readDouble();
                arcs.put(startPointIndex, new Arc(startPointIndex, v1, v2, bits));
            } else if (segmentType == EsriShpConstants.segmentLine) {
            // will never appear; should be ignored
            } else if (segmentType == EsriShpConstants.segmentSpiral) {
            } else if (segmentType == EsriShpConstants.segmentBezier3Curve) {
                // two middle control points
                double skip1 = dis.readDouble();
                double skip2 = dis.readDouble();
                double skip3 = dis.readDouble();
                double skip4 = dis.readDouble();
            } else if (segmentType == EsriShpConstants.segmentEllipticArc) {
                // center
                double skip1 = dis.readDouble();
                double skip2 = dis.readDouble();
                // rotation or fromV
                double skip3 = dis.readDouble();
                // semiMajor
                double skip4 = dis.readDouble();
                // minorMajorRatio or deltaV
                double skip5 = dis.readDouble();
                // bits
                int skip6 = dis.readInt();
            } else if (segmentType == 0) {
                break;
            } else {
                throw new IoxException("unexpected segmentType " + segmentType);
            // EhiLogger.traceState(("unexpected segmentType "+segmentType));
            // continue;
            }
        }
    }
    JtsextGeometryFactory fact = new JtsextGeometryFactory();
    if (geometryType == EsriShpConstants.ShapePolyline || geometryType == EsriShpConstants.ShapePolylineZM || geometryType == EsriShpConstants.ShapePolylineZ || geometryType == EsriShpConstants.ShapeGeneralPolyline) {
        if (cParts == 1) {
            LineString line = getPolyline(fact, 0, points, partStart, arcs, false);
            IomObject ret;
            try {
                ret = Jtsext2iox.JTS2polyline(line);
            } catch (Iox2jtsException e) {
                throw new IoxException(e);
            }
            return ret;
        }
        IomObject ret = new Iom_jObject(Wkb2iox.OBJ_MULTIPOLYLINE, null);
        for (int i = 0; i < cParts; i++) {
            LineString line = getPolyline(fact, i, points, partStart, arcs, false);
            try {
                IomObject lineObj = Jtsext2iox.JTS2polyline(line);
                ret.addattrobj(Wkb2iox.ATTR_POLYLINE, lineObj);
            } catch (Iox2jtsException e) {
                throw new IoxException(e);
            }
        }
        return ret;
    } else if (geometryType == EsriShpConstants.ShapePolygon || geometryType == EsriShpConstants.ShapePolygonZM || geometryType == EsriShpConstants.ShapePolygonZ || geometryType == EsriShpConstants.ShapeGeneralPolygon) {
        if (cParts == 1) {
            LineString line = getPolyline(fact, 0, points, partStart, arcs, true);
            if (line.getCoordinateSequence().size() <= 3) {
                throw new IoxException("Not a Ring");
            }
            Polygon polygon = fact.createCurvePolygon(fact.createRing(line));
            IomObject ret;
            try {
                ret = Jtsext2iox.JTS2surface(polygon);
            } catch (Iox2jtsException e) {
                throw new IoxException(e);
            }
            return ret;
        }
        ArrayList<LineString> shells = new ArrayList<LineString>();
        ArrayList<LineString> holes = new ArrayList<LineString>();
        for (int i = 0; i < cParts; i++) {
            LineString line = getPolyline(fact, i, points, partStart, arcs, true);
            if (line.getCoordinateSequence().size() <= 3) {
                throw new IoxException("Not a Ring");
            }
            if (CGAlgorithms.isCCW(line.getCoordinates())) {
                holes.add(line);
            } else {
                shells.add(line);
            }
        }
        if (shells.size() == 0) {
            throw new IoxException("polygon without shell");
        } else if (shells.size() == 1) {
            LinearRing shell = fact.createRing(shells.get(0));
            Polygon polygon = null;
            if (holes.size() == 0) {
                polygon = fact.createPolygon(shell);
            } else {
                LinearRing[] hole = new LinearRing[holes.size()];
                int i = 0;
                for (LineString line : holes) {
                    hole[i] = fact.createRing(line);
                    i++;
                }
                polygon = fact.createPolygon(shell, hole);
            }
            IomObject ret = null;
            try {
                ret = Jtsext2iox.JTS2surface(polygon);
            } catch (Iox2jtsException e) {
                throw new IoxException(e);
            }
            return ret;
        } else {
        // TODO  MultiSurface
        }
        return null;
    } else {
        throw new IoxException("unexpected geometryType " + geometryType);
    }
}
Also used : ArrayList(java.util.ArrayList) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) JtsextGeometryFactory(ch.interlis.iom_j.itf.impl.jtsext.geom.JtsextGeometryFactory) CurvePolygon(ch.interlis.iom_j.itf.impl.jtsext.geom.CurvePolygon) Polygon(com.vividsolutions.jts.geom.Polygon) LinearRing(com.vividsolutions.jts.geom.LinearRing) IoxException(ch.interlis.iox.IoxException) Iox2jtsException(ch.interlis.iox_j.jts.Iox2jtsException)

Example 2 with IoxException

use of ch.interlis.iox.IoxException in project ili2db by claeis.

the class TransferToXtf method doit.

public void doit(String filename, IoxWriter iomFile, String sender, String[] exportParamModelnames, long[] basketSqlIds, Map<Long, BasketStat> stat) throws ch.interlis.iox.IoxException {
    this.basketStat = stat;
    boolean referrs = false;
    if (iomFile instanceof ItfWriter) {
        config.setValue(ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_LINETABLES, ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_LINETABLES_DO);
    }
    if (config.getVer4_translation() || config.getIli1Translation() != null) {
        languageFilter = new TranslateToTranslation(td, config);
    }
    if (config.getExportModels() != null) {
        List<Model> exportModels = Ili2db.getModels(config.getExportModels(), td);
        exportBaseModelFilter = new ReduceToBaseModel(exportModels, td, config);
    }
    if (config.isValidation()) {
        ValidationConfig modelConfig = new ValidationConfig();
        modelConfig.mergeIliMetaAttrs(td);
        String configFilename = config.getValidConfigFile();
        if (configFilename != null) {
            try {
                modelConfig.mergeConfigFile(new File(configFilename));
            } catch (FileNotFoundException e) {
                EhiLogger.logError("validator config file <" + configFilename + "> not found");
            }
        }
        modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.AREA_OVERLAP_VALIDATION, config.isDisableAreaValidation() ? ValidationConfig.OFF : null);
        modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.DEFAULT_GEOMETRY_TYPE_VALIDATION, config.isSkipGeometryErrors() ? ValidationConfig.OFF : null);
        modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.ALLOW_ONLY_MULTIPLICITY_REDUCTION, config.isOnlyMultiplicityReduction() ? ValidationConfig.ON : null);
        IoxLogging errHandler = new ch.interlis.iox_j.logging.Log2EhiLogger();
        LogEventFactory errFactory = new LogEventFactory();
        errFactory.setDataSource(filename);
        if (iomFile instanceof Iligml10Writer || iomFile instanceof Iligml20Writer) {
            String crsAuthority = config.getDefaultSrsAuthority();
            String crsCode = config.getDefaultSrsCode();
            if (crsAuthority != null && crsCode != null) {
                if (iomFile instanceof Iligml10Writer) {
                    ((Iligml10Writer) iomFile).setDefaultCrs(crsAuthority + ":" + crsCode);
                } else if (iomFile instanceof Iligml20Writer) {
                    ((Iligml20Writer) iomFile).setDefaultCrs(crsAuthority + ":" + crsCode);
                }
            }
        }
        PipelinePool pipelinePool = new PipelinePool();
        validator = new ch.interlis.iox_j.validator.Validator(td, modelConfig, errHandler, errFactory, pipelinePool, config);
    }
    StartTransferEvent startEvent = new StartTransferEvent();
    startEvent.setSender(sender);
    if (languageFilter != null) {
        startEvent = (StartTransferEvent) languageFilter.filter(startEvent);
    }
    if (exportBaseModelFilter != null) {
        startEvent = (StartTransferEvent) exportBaseModelFilter.filter(startEvent);
    }
    if (validator != null)
        validator.validate(startEvent);
    iomFile.write(startEvent);
    if (basketSqlIds != null) {
        for (long basketSqlId : basketSqlIds) {
            StringBuilder basketXtfId = new StringBuilder();
            Topic topic = getTopicByBasketId(basketSqlId, basketXtfId);
            if (topic == null) {
                throw new IoxException("no basketId " + basketSqlId + " in db");
            } else {
                referrs = referrs || doBasket(filename, iomFile, topic, basketSqlId, basketXtfId.toString());
            }
        }
    } else {
        // for all MODELs
        for (String modelName : exportParamModelnames) {
            Object mObj = td.getElement(Model.class, modelName);
            if (mObj != null && (mObj instanceof Model) && !(suppressModel((Model) mObj))) {
                Model model = (Model) mObj;
                // for all TOPICs
                Iterator topici = model.iterator();
                while (topici.hasNext()) {
                    Object tObj = topici.next();
                    if (tObj instanceof Topic && !(suppressTopic((Topic) tObj))) {
                        Topic topic = (Topic) tObj;
                        referrs = referrs || doBasket(filename, iomFile, topic, null, topic.getScopedName(null));
                    }
                }
            }
        }
    }
    if (referrs) {
        throw new IoxException("dangling references");
    }
    EndTransferEvent endEvent = new EndTransferEvent();
    if (languageFilter != null) {
        endEvent = (EndTransferEvent) languageFilter.filter(endEvent);
    }
    if (exportBaseModelFilter != null) {
        endEvent = (EndTransferEvent) exportBaseModelFilter.filter(endEvent);
    }
    if (validator != null)
        validator.validate(endEvent);
    iomFile.write(endEvent);
    if (validator != null)
        validator.close();
    if (languageFilter != null) {
        languageFilter.close();
    }
    if (exportBaseModelFilter != null) {
        exportBaseModelFilter.close();
    }
}
Also used : IoxLogging(ch.interlis.iox.IoxLogging) FileNotFoundException(java.io.FileNotFoundException) LogEventFactory(ch.interlis.iox_j.logging.LogEventFactory) TranslateToTranslation(ch.interlis.iox_j.filter.TranslateToTranslation) Iligml20Writer(ch.interlis.iom_j.iligml.Iligml20Writer) Iligml10Writer(ch.interlis.iom_j.iligml.Iligml10Writer) EndTransferEvent(ch.interlis.iox_j.EndTransferEvent) Iterator(java.util.Iterator) Topic(ch.interlis.ili2c.metamodel.Topic) StartTransferEvent(ch.interlis.iox_j.StartTransferEvent) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) ValidationConfig(ch.interlis.iox_j.validator.ValidationConfig) PipelinePool(ch.interlis.iox_j.PipelinePool) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) PredefinedModel(ch.interlis.ili2c.metamodel.PredefinedModel) TypeModel(ch.interlis.ili2c.metamodel.TypeModel) Model(ch.interlis.ili2c.metamodel.Model) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) ItfWriter(ch.interlis.iom_j.itf.ItfWriter) File(java.io.File) IoxException(ch.interlis.iox.IoxException)

Example 3 with IoxException

use of ch.interlis.iox.IoxException in project ili2db by claeis.

the class TransferToXtf method getTopicByBasketId.

private Topic getTopicByBasketId(long basketSqlId, StringBuilder basketXtfId) throws IoxException {
    String sqlName = DbNames.BASKETS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    String topicName = null;
    String bid = null;
    java.sql.PreparedStatement getstmt = null;
    try {
        String stmt = "SELECT " + DbNames.BASKETS_TAB_TOPIC_COL + "," + DbNames.T_ILI_TID_COL + " FROM " + sqlName + " WHERE " + colT_ID + "= ?";
        EhiLogger.traceBackendCmd(stmt);
        getstmt = conn.prepareStatement(stmt);
        getstmt.setLong(1, basketSqlId);
        java.sql.ResultSet res = getstmt.executeQuery();
        if (res.next()) {
            topicName = res.getString(1);
            bid = res.getString(2);
        }
    } catch (java.sql.SQLException ex) {
        EhiLogger.logError("failed to query " + sqlName, ex);
    } finally {
        if (getstmt != null) {
            try {
                getstmt.close();
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError(ex);
            }
        }
    }
    if (topicName != null) {
        Topic topic = getTopicDef(td, topicName);
        if (topic == null) {
            throw new IoxException("unknown Topic " + topicName + " in table " + sqlName);
        }
        basketXtfId.append(bid);
        return topic;
    }
    return null;
}
Also used : Topic(ch.interlis.ili2c.metamodel.Topic) IoxException(ch.interlis.iox.IoxException)

Example 4 with IoxException

use of ch.interlis.iox.IoxException in project ili2db by claeis.

the class DbUtilityExecuteSqlTest method createSchema.

private void createSchema(ResultSet rs, Statement stmt) throws IoxException {
    try {
        stmt.execute("CREATE SCHEMA " + DBSCHEMA);
        rs = stmt.executeQuery("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '" + DBSCHEMA + "'");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        assertEquals(1, columnCount);
        while (rs.next()) {
            for (int i = 1; i <= columnCount; i++) {
                assertEquals(DBSCHEMA, rs.getString(1));
            }
        }
        if (rs != null) {
            rs.close();
            rs = null;
        }
    } catch (SQLException e) {
        throw new IoxException(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) IoxException(ch.interlis.iox.IoxException)

Example 5 with IoxException

use of ch.interlis.iox.IoxException in project ili2db by claeis.

the class DbUtilityExecuteSqlTest method dropSchema.

private void dropSchema(ResultSet rs, Statement stmt) throws IoxException {
    try {
        stmt.execute("DROP SCHEMA IF EXISTS " + DBSCHEMA + " CASCADE");
        rs = stmt.executeQuery("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '" + DBSCHEMA + "'");
        assertFalse(rs.next());
        if (rs != null) {
            rs.close();
            rs = null;
        }
    } catch (SQLException e) {
        throw new IoxException(e);
    }
}
Also used : SQLException(java.sql.SQLException) IoxException(ch.interlis.iox.IoxException)

Aggregations

IoxException (ch.interlis.iox.IoxException)32 File (java.io.File)16 Test (org.junit.Test)16 SQLException (java.sql.SQLException)15 Config (ch.ehi.ili2db.gui.Config)14 Connection (java.sql.Connection)13 IomObject (ch.interlis.iom.IomObject)12 HashMap (java.util.HashMap)9 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)8 XtfReader (ch.interlis.iom_j.xtf.XtfReader)8 EndBasketEvent (ch.interlis.iox.EndBasketEvent)8 EndTransferEvent (ch.interlis.iox.EndTransferEvent)8 IoxEvent (ch.interlis.iox.IoxEvent)8 StartTransferEvent (ch.interlis.iox.StartTransferEvent)8 ConverterException (ch.ehi.ili2db.converter.ConverterException)7 ObjectEvent (ch.interlis.iox.ObjectEvent)7 StartBasketEvent (ch.interlis.iox.StartBasketEvent)7 ResultSet (java.sql.ResultSet)6 ResultSetMetaData (java.sql.ResultSetMetaData)6 ParseException (com.vividsolutions.jts.io.ParseException)5