Search in sources :

Example 1 with XBase

use of org.pentaho.di.trans.steps.xbaseinput.XBase in project pentaho-kettle by pentaho.

the class ShapeFileReaderMeta method getFields.

public void getFields(RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space) throws KettleStepException {
    // The filename...
    ValueMetaInterface filename = new ValueMeta("filename", ValueMetaInterface.TYPE_STRING);
    filename.setOrigin(name);
    filename.setLength(255);
    row.addValueMeta(filename);
    // The file type
    ValueMetaInterface ft = new ValueMeta("filetype", ValueMetaInterface.TYPE_STRING);
    ft.setLength(50);
    ft.setOrigin(name);
    row.addValueMeta(ft);
    // The shape nr
    ValueMetaInterface shnr = new ValueMeta("shapenr", ValueMetaInterface.TYPE_INTEGER);
    shnr.setOrigin(name);
    row.addValueMeta(shnr);
    // The part nr
    ValueMetaInterface pnr = new ValueMeta("partnr", ValueMetaInterface.TYPE_INTEGER);
    pnr.setOrigin(name);
    row.addValueMeta(pnr);
    // The part nr
    ValueMetaInterface nrp = new ValueMeta("nrparts", ValueMetaInterface.TYPE_INTEGER);
    nrp.setOrigin(name);
    row.addValueMeta(nrp);
    // The point nr
    ValueMetaInterface ptnr = new ValueMeta("pointnr", ValueMetaInterface.TYPE_INTEGER);
    ptnr.setOrigin(name);
    row.addValueMeta(ptnr);
    // The nr of points
    ValueMetaInterface nrpt = new ValueMeta("nrpointS", ValueMetaInterface.TYPE_INTEGER);
    nrpt.setOrigin(name);
    row.addValueMeta(nrpt);
    // The X coordinate
    ValueMetaInterface x = new ValueMeta("x", ValueMetaInterface.TYPE_NUMBER);
    x.setOrigin(name);
    row.addValueMeta(x);
    // The Y coordinate
    ValueMetaInterface y = new ValueMeta("y", ValueMetaInterface.TYPE_NUMBER);
    y.setOrigin(name);
    row.addValueMeta(y);
    // The measure
    ValueMetaInterface m = new ValueMeta("measure", ValueMetaInterface.TYPE_NUMBER);
    m.setOrigin(name);
    row.addValueMeta(m);
    if (getDbfFilename() != null) {
        XBase xbase = new XBase(getLog(), getDbfFilename());
        try {
            xbase.setDbfFile(getDbfFilename());
            xbase.open();
            RowMetaInterface fields = xbase.getFields();
            for (int i = 0; i < fields.size(); i++) {
                fields.getValueMeta(i).setOrigin(name);
                row.addValueMeta(fields.getValueMeta(i));
            }
        } catch (Throwable e) {
            throw new KettleStepException("Unable to read from DBF file", e);
        } finally {
            xbase.close();
        }
    } else {
        throw new KettleStepException("Unable to read from DBF file: no filename specfied");
    }
}
Also used : KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMeta(org.pentaho.di.core.row.ValueMeta) XBase(org.pentaho.di.trans.steps.xbaseinput.XBase) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 2 with XBase

use of org.pentaho.di.trans.steps.xbaseinput.XBase in project pentaho-kettle by pentaho.

the class ShapeFile method readFile.

public void readFile() throws GisException, KettleException {
    File file = new File(shapeFilename);
    try {
        // Open shape file & DBF file...
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        XBase xbase = new XBase(log, dbfFilename);
        // throws exception now
        xbase.open();
        // First determine the meta-data for this dbf file...
        RowMetaInterface fields = xbase.getFields();
        // Read the header data...
        byte[] header = new byte[100];
        dis.read(header);
        fileheader = new ShapeFileHeader(header);
        int id = 0;
        while (dis.available() > 0) {
            // Read the record header to see the length of the next shape...
            byte[] record_header = new byte[8];
            dis.read(record_header);
            ShapeRecordHeader erh = new ShapeRecordHeader(record_header);
            // Read the actual content of the shape
            if (erh.length <= dis.available()) {
                byte[] content = new byte[erh.length];
                dis.read(content);
                // Determine the shape type...
                int btype = Converter.getIntegerLittle(content, 0);
                ShapeInterface esi = null;
                switch(btype) {
                    case Shape.SHAPE_TYPE_NULL:
                        esi = new ShapeNull(content);
                        break;
                    case Shape.SHAPE_TYPE_POINT:
                        esi = new ShapePoint(content);
                        break;
                    case Shape.SHAPE_TYPE_POLYLINE:
                        esi = new ShapePolyLine(content);
                        break;
                    case Shape.SHAPE_TYPE_POLYGON:
                        esi = new ShapePolygon(content);
                        break;
                    case Shape.SHAPE_TYPE_POLYLINE_M:
                        esi = new ShapePolyLineM(content);
                        break;
                    default:
                        throw new GisException("shape type : " + btype + " not recognized! (" + Shape.getEsriTypeDesc(btype) + ")");
                }
                // Get a row from the associated DBF file...
                Object[] row = xbase.getRow(fields);
                if (row != null) {
                    esi.setDbfData(row);
                    esi.setDbfMeta(xbase.getFields());
                }
                shapes.add(esi);
                id++;
            }
        }
        dis.close();
        xbase.close();
    } catch (IOException e) {
        throw new GisException("Error reading shape file", e);
    }
}
Also used : RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) File(java.io.File) XBase(org.pentaho.di.trans.steps.xbaseinput.XBase)

Aggregations

RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 XBase (org.pentaho.di.trans.steps.xbaseinput.XBase)2 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 KettleStepException (org.pentaho.di.core.exception.KettleStepException)1 ValueMeta (org.pentaho.di.core.row.ValueMeta)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1