Search in sources :

Example 6 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class TransferToXtf method doitJava.

public void doitJava() {
    try {
        expgen = new ch.interlis.ili2c.generator.IndentPrintWriter(new java.io.BufferedWriter(new java.io.FileWriter("c:/tmp/ili2db/export.java")));
        expgen.println("import ch.ehi.basics.logging.EhiLogger;");
        expgen.println("import ch.interlis.iom.IomObject;");
        expgen.println("public class TransferToXtf implements IdMapper {");
        expgen.indent();
        expgen.println("private java.sql.Connection conn=null;");
        expgen.println("private IomObject newObject(String className,String tid)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO create new object");
        expgen.println("return null;");
        expgen.unindent();
        expgen.println("}");
        expgen.println("private void writeObject(IomObject iomObj)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO write object");
        expgen.unindent();
        expgen.println("}");
        expgen.println("public String mapId(String idSpace,String id)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO mapId");
        expgen.println("return id;");
        expgen.unindent();
        expgen.println("}");
        expgen.println("public String newId()");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO newId");
        expgen.println("throw new UnsupportedOperationException(\"this mapper doesn't generate new ids\");");
        expgen.unindent();
        expgen.println("}");
    } catch (java.io.IOException ex) {
        EhiLogger.logError("failed to open file for java output", ex);
    }
    try {
        // for all MODELs
        Iterator modeli = td.iterator();
        while (modeli.hasNext()) {
            Object mObj = modeli.next();
            if ((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;
                        // IomBasket iomBasket=null;
                        // for all Viewables
                        Iterator iter = topic.getViewables().iterator();
                        while (iter.hasNext()) {
                            Object obj = iter.next();
                            if ((obj instanceof Viewable) && !suppressViewableIfJava((Viewable) obj)) {
                                Viewable aclass = (Viewable) obj;
                                // dump it
                                EhiLogger.logState(aclass.getScopedName(null) + "...");
                                genClassHelper(aclass);
                            } else if (obj instanceof Domain) {
                                Domain domainDef = (Domain) obj;
                                if (domainDef.getType() instanceof EnumerationType) {
                                    String enumName = domainDef.getScopedName(null);
                                    enumTypes.add(enumName);
                                }
                            }
                        }
                    } else if ((tObj instanceof Viewable) && !suppressViewableIfJava((Viewable) tObj)) {
                        Viewable aclass = (Viewable) tObj;
                        // dump it
                        EhiLogger.logState(aclass.getScopedName(null) + "...");
                        genClassHelper(aclass);
                    } else if (tObj instanceof Domain) {
                        Domain domainDef = (Domain) tObj;
                        if (domainDef.getType() instanceof EnumerationType) {
                            String enumName = domainDef.getScopedName(null);
                            enumTypes.add(enumName);
                        }
                    }
                }
            }
        }
    } finally {
        if (expgen != null) {
            {
                expgen.println("private void addAny(String iliClassName,String select){");
                expgen.indent();
                Iterator linei = addanyLines.iterator();
                String sep = "";
                while (linei.hasNext()) {
                    String line = (String) linei.next();
                    expgen.println(sep + line);
                    sep = "else ";
                }
                if (sep.length() > 0) {
                    expgen.println("else{EhiLogger.logError(\"unknown class \"+iliClassName);}");
                } else {
                    expgen.println("EhiLogger.logError(\"unknown class \"+iliClassName);");
                }
                expgen.unindent();
                expgen.println("}");
            }
            {
                expgen.println("private EnumMapper getEnumMapper(String enumName){");
                expgen.indent();
                Iterator enumi = enumTypes.iterator();
                String sep = "";
                while (enumi.hasNext()) {
                    String enumName = (String) enumi.next();
                    expgen.println(sep + "if(enumName.equals(\"" + enumName + "\")){return new IdentityEnumMapper();}");
                    sep = "else ";
                }
                if (sep.length() > 0) {
                    expgen.println("else{throw new IllegalArgumentException(\"unknown enum \"+enumName);}");
                } else {
                    expgen.println("throw new IllegalArgumentException(\"unknown enum \"+enumName);");
                }
                expgen.unindent();
                expgen.println("}");
            }
            expgen.unindent();
            expgen.println("}");
            expgen.close();
        }
    }
}
Also used : EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) Iterator(java.util.Iterator) 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) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) Topic(ch.interlis.ili2c.metamodel.Topic) Domain(ch.interlis.ili2c.metamodel.Domain)

Example 7 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class OracleColumnConverter method toIomPolyline.

@Override
public IomObject toIomPolyline(Object geomobj, String sqlAttrName, boolean is3D) throws java.sql.SQLException, ConverterException {
    JGeometry geometry = JGeometry.load((oracle.sql.STRUCT) geomobj);
    if (geometry.getType() != JGeometry.GTYPE_MULTICURVE) {
        throw new ConverterException("unexpected GTYPE (" + OracleUtility.gtype2str(geometry.getType()) + ") in attribute " + sqlAttrName);
    } else {
        int dim = geometry.getDimensions();
        boolean isCurrentValue3D = (dim == 3);
        if (is3D != isCurrentValue3D) {
            throw new ConverterException("unexpected dimension (" + Integer.toString(dim) + ") in attribute " + sqlAttrName);
        } else {
            int[] elev = geometry.getElemInfo();
            double[] ordv = geometry.getOrdinatesArray();
            final int SDO_STARTING_OFFSET = 0;
            final int SDO_ETYPE = 1;
            final int SDO_INTERPRETATION = 2;
            final int NEXT_TRIPLET = 3;
            final int COMPOUND_LINESTRING = 4;
            final int LINESTRING = 2;
            final int STRAIGHT = 1;
            final int ARC = 2;
            int elei = 0;
            if (elev[elei + SDO_ETYPE] != COMPOUND_LINESTRING) {
                throw new ConverterException("unexpected SDO_ETYPE (" + Integer.toString(elev[elei + SDO_ETYPE]) + ") in attribute " + sqlAttrName);
            } else {
                elei += NEXT_TRIPLET;
                IomObject polylineValue = new Iom_jObject("POLYLINE", null);
                // unclipped polyline, add one sequence
                IomObject sequence = polylineValue.addattrobj("sequence", "SEGMENTS");
                while (elei < elev.length) {
                    if (elev[elei + SDO_ETYPE] != LINESTRING) {
                        throw new ConverterException("unexpected SDO_ETYPE (" + Integer.toString(elev[elei + SDO_ETYPE]) + ") in attribute " + sqlAttrName);
                    } else {
                        if (elev[elei + SDO_INTERPRETATION] == STRAIGHT) {
                            int start = elev[elei + SDO_STARTING_OFFSET] - 1;
                            int end;
                            if (elei + NEXT_TRIPLET >= elev.length) {
                                end = ordv.length;
                            } else {
                                end = elev[elei + NEXT_TRIPLET + SDO_STARTING_OFFSET] - 1;
                            }
                            for (int i = start; i < end; ) {
                                // add control point
                                IomObject coordValue = sequence.addattrobj("segment", "COORD");
                                coordValue.setattrvalue("C1", Double.toString(ordv[i]));
                                coordValue.setattrvalue("C2", Double.toString(ordv[i + 1]));
                                if (isCurrentValue3D) {
                                    coordValue.setattrvalue("C3", Double.toString(ordv[i + 2]));
                                    i += 3;
                                } else {
                                    i += 2;
                                }
                            }
                        } else if (elev[elei + SDO_INTERPRETATION] == ARC) {
                            int start = elev[elei + SDO_STARTING_OFFSET] - 1;
                            int end;
                            if (elei + NEXT_TRIPLET >= elev.length) {
                                end = ordv.length;
                            } else {
                                end = elev[elei + NEXT_TRIPLET + SDO_STARTING_OFFSET] - 1;
                            }
                            for (int i = start; i < end; ) {
                                // add control point
                                IomObject coordValue = sequence.addattrobj("segment", "ARC");
                                coordValue.setattrvalue("A1", Double.toString(ordv[i]));
                                coordValue.setattrvalue("A2", Double.toString(ordv[i + 1]));
                                if (isCurrentValue3D) {
                                    // no A3 in XTF!
                                    i += 3;
                                } else {
                                    i += 2;
                                }
                                coordValue.setattrvalue("C1", Double.toString(ordv[i]));
                                coordValue.setattrvalue("C2", Double.toString(ordv[i + 1]));
                                if (isCurrentValue3D) {
                                    coordValue.setattrvalue("C3", Double.toString(ordv[i + 2]));
                                    i += 3;
                                } else {
                                    i += 2;
                                }
                            }
                        } else {
                            throw new ConverterException("unexpected SDO_INTERPRETATION (" + Integer.toString(elev[elei + SDO_INTERPRETATION]) + ") in attribute " + sqlAttrName);
                        }
                    }
                    elei += NEXT_TRIPLET;
                }
                return polylineValue;
            }
        }
    }
}
Also used : ConverterException(ch.ehi.ili2db.converter.ConverterException) IomObject(ch.interlis.iom.IomObject) JGeometry(oracle.spatial.geometry.JGeometry) Iom_jObject(ch.interlis.iom_j.Iom_jObject)

Example 8 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class OracleColumnConverter method addPolyline.

private void addPolyline(ArrayList elemInfo, ArrayList ordinates, IomObject obj, boolean hasLineAttr, boolean is3D) throws java.sql.SQLException, ConverterException {
    is3D = false;
    if (obj != null) {
        final int LINESTRING = 2;
        final int STRAIGHT = 1;
        final int ARC = 2;
        int lastSegment = 0;
        if (hasLineAttr) {
            IomObject lineattr = obj.getattrobj("lineattr", 0);
            if (lineattr != null) {
                // writeAttrs(out,lineattr);
                EhiLogger.logAdaption("Lineattributes not supported by Oracle; ignored");
            }
        }
        boolean clipped = obj.getobjectconsistency() == IomConstants.IOM_INCOMPLETE;
        for (int sequencei = 0; sequencei < obj.getattrvaluecount("sequence"); sequencei++) {
            if (clipped) {
            // out.startElement(tags::get_CLIPPED(),0,0);
            } else {
                // an unclipped polyline should have only one sequence element
                if (sequencei > 0) {
                    EhiLogger.logError("unclipped polyline with multi 'sequence' elements");
                    break;
                }
            }
            IomObject sequence = obj.getattrobj("sequence", sequencei);
            for (int segmenti = 0; segmenti < sequence.getattrvaluecount("segment"); segmenti++) {
                IomObject segment = sequence.getattrobj("segment", segmenti);
                if (segment.getobjecttag().equals("COORD")) {
                    // COORD
                    if (lastSegment != STRAIGHT) {
                        addElemInfo(elemInfo, ordinates.size(), LINESTRING, STRAIGHT);
                    }
                    String c1 = segment.getattrvalue("C1");
                    String c2 = segment.getattrvalue("C2");
                    String c3 = segment.getattrvalue("C3");
                    ordinates.add(Double.valueOf(c1));
                    ordinates.add(Double.valueOf(c2));
                    if (is3D) {
                        ordinates.add(Double.valueOf(c3));
                    }
                } else if (segment.getobjecttag().equals("ARC")) {
                    // ARC
                    if (lastSegment != ARC) {
                        addElemInfo(elemInfo, ordinates.size(), LINESTRING, ARC);
                    }
                    String c1 = segment.getattrvalue("C1");
                    String c2 = segment.getattrvalue("C2");
                    String c3 = segment.getattrvalue("C3");
                    String a1 = segment.getattrvalue("A1");
                    String a2 = segment.getattrvalue("A2");
                    ordinates.add(Double.valueOf(a1));
                    ordinates.add(Double.valueOf(a2));
                    if (is3D) {
                        // TODO calculate a3
                        throw new IllegalStateException("TODO: calculate a3");
                    // ordinates.add(Double.valueOf(a3));
                    }
                    ordinates.add(Double.valueOf(c1));
                    ordinates.add(Double.valueOf(c2));
                    if (is3D) {
                        ordinates.add(Double.valueOf(c3));
                    }
                } else {
                    // custum line form
                    EhiLogger.logAdaption("custom line form not supported by Oracle; ignored");
                // out.startElement(segment->getTag(),0,0);
                // writeAttrs(out,segment);
                // out.endElement(/*segment*/);
                }
            }
            if (clipped) {
            // out.endElement(/*CLIPPED*/);
            }
        }
    }
}
Also used : IomObject(ch.interlis.iom.IomObject)

Example 9 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class BlackBoxTypes23Test method exportXtf.

@Test
public void exportXtf() throws Exception {
    Connection jdbcConnection = null;
    try {
        Class driverClass = Class.forName("org.postgresql.Driver");
        jdbcConnection = DriverManager.getConnection(dburl, dbuser, dbpwd);
        stmt = jdbcConnection.createStatement();
        stmt.execute("DROP SCHEMA IF EXISTS " + DBSCHEMA + " CASCADE");
        DbUtility.executeSqlScript(jdbcConnection, new java.io.FileReader("test/data/BlackBoxTypes23/CreateTable.sql"));
        DbUtility.executeSqlScript(jdbcConnection, new java.io.FileReader("test/data/BlackBoxTypes23/InsertIntoTable.sql"));
        File data = new File("test/data/BlackBoxTypes23/BlackBoxTypes23a-out.xtf");
        Config config = initConfig(data.getPath(), DBSCHEMA, data.getPath() + ".log");
        config.setModels("BlackBoxTypes23");
        config.setFunction(Config.FC_EXPORT);
        Ili2db.readSettingsFromDb(config);
        Ili2db.run(config, null);
        // read objects of db and write objectValue to HashMap
        HashMap<String, IomObject> objs = new HashMap<String, IomObject>();
        XtfReader reader = new XtfReader(data);
        IoxEvent event = null;
        do {
            event = reader.read();
            if (event instanceof StartTransferEvent) {
            } else if (event instanceof StartBasketEvent) {
            } else if (event instanceof ObjectEvent) {
                IomObject iomObj = ((ObjectEvent) event).getIomObject();
                if (iomObj.getobjectoid() != null) {
                    objs.put(iomObj.getobjectoid(), iomObj);
                }
            } else if (event instanceof EndBasketEvent) {
            } else if (event instanceof EndTransferEvent) {
            }
        } while (!(event instanceof EndTransferEvent));
        {
            IomObject obj0 = objs.get("o0");
            Assert.assertNotNull(obj0);
            Assert.assertEquals("BlackBoxTypes23.Topic.ClassA", obj0.getobjecttag());
        }
        {
            IomObject obj1 = objs.get("o1");
            Assert.assertNotNull(obj1);
            Assert.assertEquals("BlackBoxTypes23.Topic.ClassA", obj1.getobjecttag());
            Assert.assertEquals("AAAA", obj1.getattrvalue("binbox"));
        }
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
    }
}
Also used : ObjectEvent(ch.interlis.iox.ObjectEvent) HashMap(java.util.HashMap) Config(ch.ehi.ili2db.gui.Config) Connection(java.sql.Connection) StartTransferEvent(ch.interlis.iox.StartTransferEvent) EndBasketEvent(ch.interlis.iox.EndBasketEvent) IomObject(ch.interlis.iom.IomObject) EndTransferEvent(ch.interlis.iox.EndTransferEvent) XtfReader(ch.interlis.iom_j.xtf.XtfReader) IoxEvent(ch.interlis.iox.IoxEvent) StartBasketEvent(ch.interlis.iox.StartBasketEvent) File(java.io.File) Test(org.junit.Test)

Example 10 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class Iox2fgdb method multisurface2wkb.

public byte[] multisurface2wkb(// SurfaceOrAreaType type)
IomObject multisurfaceObj, // SurfaceOrAreaType type)
boolean asCurvePolygon, // SurfaceOrAreaType type)
double strokeP, // SurfaceOrAreaType type)
int srsId) throws IoxException {
    if (multisurfaceObj == null) {
        return null;
    }
    byte[] ret = null;
    os.reset();
    if (asCurvePolygon) {
        int shapeType = EsriShpConstants.ShapeGeneralPolygon;
        shapeType |= EsriShpConstants.shapeHasCurves;
        shapeType |= (outputDimension == 3 ? EsriShpConstants.shapeHasZs : 0);
        os.writeInt(shapeType);
    } else {
        if (outputDimension == 3) {
            os.writeInt(EsriShpConstants.ShapePolygonZ);
        } else {
            os.writeInt(EsriShpConstants.ShapePolygon);
        }
    }
    java.util.ArrayList<Polygon> polygons = new java.util.ArrayList<Polygon>();
    // boundingBox
    Envelope env = new Envelope();
    int surfacec = multisurfaceObj.getattrvaluecount("surface");
    for (int surfacei = 0; surfacei < surfacec; surfacei++) {
        IomObject surface = multisurfaceObj.getattrobj("surface", surfacei);
        IomObject iomSurfaceClone = new ch.interlis.iom_j.Iom_jObject("MULTISURFACE", null);
        iomSurfaceClone.addattrobj("surface", surface);
        Polygon polygon = Iox2jtsext.surface2JTS(iomSurfaceClone, strokeP);
        polygons.add(polygon);
        env.expandToInclude(polygon.getEnvelopeInternal());
    }
    os.writeDouble(env.getMinX());
    os.writeDouble(env.getMinY());
    os.writeDouble(env.getMaxX());
    os.writeDouble(env.getMaxY());
    // cParts The number of rings in the multisurface.
    // cPoints The total number of points for all parts.
    int cPart = 0;
    int cPoints = 0;
    for (Polygon polygon : polygons) {
        cPart += 1;
        cPoints += getNumPoints(polygon.getExteriorRing());
        int holec = polygon.getNumInteriorRing();
        cPart += holec;
        for (int holei = 0; holei < holec; holei++) {
            cPoints += getNumPoints(polygon.getInteriorRingN(holei));
        }
    }
    os.writeInt(cPart);
    os.writeInt(cPoints);
    // parts[cParts] An array of length NumParts. Stores, for each Ring, the index of its
    // first point in the points array. Array indexes are with respect to 0.
    int partStart = 0;
    for (Polygon polygon : polygons) {
        os.writeInt(partStart);
        partStart += getNumPoints(polygon.getExteriorRing());
        int holec = polygon.getNumInteriorRing();
        for (int holei = 0; holei < holec; holei++) {
            os.writeInt(partStart);
            partStart += getNumPoints(polygon.getInteriorRingN(holei));
        }
    }
    java.util.ArrayList<Arc> arcs = null;
    if (asCurvePolygon) {
        arcs = new java.util.ArrayList<Arc>();
    }
    java.util.ArrayList<Double> zv = null;
    if (outputDimension == 3) {
        zv = new java.util.ArrayList<Double>();
    }
    double[] zMin = new double[1];
    double[] zMax = new double[1];
    {
        Coordinate coord = polygons.get(0).getExteriorRing().getStartPoint().getCoordinate();
        if (outputDimension == 3) {
            zMin[0] = coord.z;
            zMax[0] = coord.z;
        }
    }
    int startPtIdx = 0;
    for (Polygon polygon : polygons) {
        // shell is always in clockwise order
        // holes are in a counterclockwise direction
        LineString polyline = polygon.getExteriorRing();
        polyline = asOneLine(polyline);
        if (CGAlgorithms.isCCW(polyline.getCoordinates())) {
            polyline = (LineString) polyline.reverse();
        }
        writePoints(polyline, false, zv, zMin, zMax, startPtIdx, arcs);
        startPtIdx += getNumPoints(polyline);
        int holec = polygon.getNumInteriorRing();
        for (int holei = 0; holei < holec; holei++) {
            polyline = polygon.getInteriorRingN(holei);
            polyline = asOneLine(polyline);
            if (!CGAlgorithms.isCCW(polyline.getCoordinates())) {
                polyline = (LineString) polyline.reverse();
            }
            writePoints(polyline, true, zv, zMin, zMax, startPtIdx, arcs);
            startPtIdx += getNumPoints(polyline);
        }
    }
    if (outputDimension == 3) {
        // zMin
        os.writeDouble(zMin[0]);
        // zMax
        os.writeDouble(zMax[0]);
        // Zs[cPoints]
        for (Double z : zv) {
            os.writeDouble(z);
        }
    }
    if (asCurvePolygon) {
        writeArcs(arcs);
    }
    ret = os.toByteArray();
    return ret;
}
Also used : ArrayList(java.util.ArrayList) Envelope(com.vividsolutions.jts.geom.Envelope) IomObject(ch.interlis.iom.IomObject) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) Polygon(com.vividsolutions.jts.geom.Polygon)

Aggregations

IomObject (ch.interlis.iom.IomObject)66 EndBasketEvent (ch.interlis.iox.EndBasketEvent)35 EndTransferEvent (ch.interlis.iox.EndTransferEvent)35 IoxEvent (ch.interlis.iox.IoxEvent)35 StartTransferEvent (ch.interlis.iox.StartTransferEvent)35 File (java.io.File)35 HashMap (java.util.HashMap)35 Config (ch.ehi.ili2db.gui.Config)34 ObjectEvent (ch.interlis.iox.ObjectEvent)34 StartBasketEvent (ch.interlis.iox.StartBasketEvent)34 Connection (java.sql.Connection)34 XtfReader (ch.interlis.iom_j.xtf.XtfReader)30 Test (org.junit.Test)29 IoxException (ch.interlis.iox.IoxException)12 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)11 Iom_jObject (ch.interlis.iom_j.Iom_jObject)9 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)8 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)8 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8