Search in sources :

Example 1 with ALasWriter

use of org.hortonmachine.gears.io.las.core.ALasWriter in project hortonmachine by TheHortonMachine.

the class FlightLinesIntensityNormalizer method process.

@Execute
public void process() throws Exception {
    checkNull(inLas, inFlightpoints, pDateTimePattern);
    int timeType = -1;
    if (pGpsTimeType.equals(FlightLinesExtractor.ADJUSTED_STANDARD_GPS_TIME)) {
        timeType = 1;
    }
    if (pGpsTimeType.equals(FlightLinesExtractor.WEEK_SECONDS_TIME)) {
        timeType = 0;
    }
    SimpleFeatureCollection flightPointsFC = OmsVectorReader.readVector(inFlightpoints);
    List<SimpleFeature> flightPointsList = FeatureUtilities.featureCollectionToList(flightPointsFC);
    SimpleFeatureType schema = flightPointsFC.getSchema();
    String dateName = FeatureUtilities.findAttributeName(schema, "date");
    String timeName = FeatureUtilities.findAttributeName(schema, "time");
    String elevName = FeatureUtilities.findAttributeName(schema, "elev");
    if (dateName == null || timeName == null || elevName == null) {
        throw new ModelsIllegalargumentException("The shapefile has to contain the fields date time and elev.", this);
    }
    pm.beginTask("Defining flight intervals and positions...", flightPointsList.size());
    DateTimeFormatter formatter = DateTimeFormat.forPattern(pDateTimePattern).withZone(DateTimeZone.UTC);
    TreeMap<DateTime, Coordinate> date2pointsMap = new TreeMap<DateTime, Coordinate>();
    TreeMap<Coordinate, DateTime> points2dateMap = new TreeMap<Coordinate, DateTime>();
    for (int i = 0; i < flightPointsList.size(); i++) {
        SimpleFeature flightPoint = flightPointsList.get(i);
        Geometry g1 = (Geometry) flightPoint.getDefaultGeometry();
        Coordinate c1 = g1.getCoordinate();
        double elev1 = ((Number) flightPoint.getAttribute(elevName)).doubleValue();
        c1.z = elev1;
        String date1 = flightPoint.getAttribute(dateName).toString();
        String time1 = flightPoint.getAttribute(timeName).toString();
        String dateTime1 = date1 + " " + time1;
        DateTime d1 = formatter.parseDateTime(dateTime1);
        date2pointsMap.put(d1, c1);
        points2dateMap.put(c1, d1);
        pm.worked(1);
    }
    pm.done();
    pm.beginTask("Create time index...", flightPointsList.size() - 1);
    DateTime minDate = null;
    DateTime maxDate = null;
    long minLong = Long.MAX_VALUE;
    long maxLong = -Long.MAX_VALUE;
    STRtree tree = new STRtree(flightPointsList.size());
    Set<Entry<DateTime, Coordinate>> pointsSet = date2pointsMap.entrySet();
    Entry[] array = pointsSet.toArray(new Entry[0]);
    for (int i = 0; i < array.length - 1; i++) {
        DateTime d1 = (DateTime) array[i].getKey();
        Coordinate c1 = (Coordinate) array[i].getValue();
        DateTime d2 = (DateTime) array[i + 1].getKey();
        Coordinate c2 = (Coordinate) array[i + 1].getValue();
        long millis1 = d1.getMillis();
        long millis2 = d2.getMillis();
        Envelope timeEnv = new Envelope(millis1, millis2, millis1, millis2);
        tree.insert(timeEnv, new Coordinate[] { c1, c2 });
        if (millis1 < minLong) {
            minLong = millis1;
            minDate = d1;
        }
        if (millis2 > maxLong) {
            maxLong = millis2;
            maxDate = d2;
        }
        pm.worked(1);
    }
    pm.done();
    StringBuilder sb = new StringBuilder();
    sb.append("Flight data interval: ");
    sb.append(minDate.toString(HMConstants.dateTimeFormatterYYYYMMDDHHMMSS));
    sb.append(" to ");
    sb.append(maxDate.toString(HMConstants.dateTimeFormatterYYYYMMDDHHMMSS));
    pm.message(sb.toString());
    CoordinateReferenceSystem crs = null;
    File lasFile = new File(inLas);
    File outLasFile = new File(outLas);
    try (// 
    ALasReader reader = ALasReader.getReader(lasFile, crs);
        ALasWriter writer = ALasWriter.getWriter(outLasFile, crs)) {
        reader.setOverrideGpsTimeType(timeType);
        ILasHeader header = reader.getHeader();
        int gpsTimeType = header.getGpsTimeType();
        writer.setBounds(header);
        writer.open();
        pm.beginTask("Interpolating flight points and normalizing...", (int) header.getRecordsCount());
        while (reader.hasNextPoint()) {
            LasRecord r = reader.getNextPoint();
            DateTime gpsTimeToDateTime;
            if (timeType == 0) {
                gpsTimeToDateTime = GpsTimeConverter.gpsWeekTime2DateTime(r.gpsTime);
            } else {
                gpsTimeToDateTime = LasUtils.adjustedStandardGpsTime2DateTime(r.gpsTime);
            }
            long gpsMillis = gpsTimeToDateTime.getMillis();
            Coordinate lasCoordinate = new Coordinate(r.x, r.y, r.z);
            Envelope pEnv = new Envelope(new Coordinate(gpsMillis, gpsMillis));
            List points = tree.query(pEnv);
            Coordinate[] flightCoords = (Coordinate[]) points.get(0);
            long d1 = points2dateMap.get(flightCoords[0]).getMillis();
            long d2 = points2dateMap.get(flightCoords[1]).getMillis();
            LineSegment line = new LineSegment(flightCoords[0], flightCoords[1]);
            double fraction = (gpsMillis - d1) / (d2 - d1);
            Coordinate interpolatedFlightPoint = line.pointAlong(fraction);
            // calculate interpolated elevation
            double distX = interpolatedFlightPoint.distance(flightCoords[0]);
            double dist12 = flightCoords[1].distance(flightCoords[0]);
            double interpolatedElev = distX / dist12 * (flightCoords[1].z - flightCoords[0].z) + flightCoords[0].z;
            interpolatedFlightPoint.z = interpolatedElev;
            double distanceFlightTerrain = GeometryUtilities.distance3d(lasCoordinate, interpolatedFlightPoint, null);
            short norm = (short) floor(r.intensity * pow(distanceFlightTerrain, 2.0) / pStdRange + 0.5);
            r.intensity = norm;
            writer.addPoint(r);
            pm.worked(1);
        }
        pm.done();
    }
}
Also used : Envelope(org.locationtech.jts.geom.Envelope) DateTime(org.joda.time.DateTime) ModelsIllegalargumentException(org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException) Entry(java.util.Map.Entry) ALasWriter(org.hortonmachine.gears.io.las.core.ALasWriter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ILasHeader(org.hortonmachine.gears.io.las.core.ILasHeader) STRtree(org.locationtech.jts.index.strtree.STRtree) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader) LineSegment(org.locationtech.jts.geom.LineSegment) TreeMap(java.util.TreeMap) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Geometry(org.locationtech.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(org.locationtech.jts.geom.Coordinate) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) File(java.io.File) Execute(oms3.annotations.Execute)

Example 2 with ALasWriter

use of org.hortonmachine.gears.io.las.core.ALasWriter in project hortonmachine by TheHortonMachine.

the class LasElevationHandler method process.

@Execute
public void process() throws Exception {
    checkNull(inFile, inDtm);
    GridCoverage2D dtm = getRaster(inDtm);
    File inLas = new File(inFile);
    try (ALasReader reader = ALasReader.getReader(inLas, null)) {
        reader.open();
        ILasHeader header = reader.getHeader();
        long recordsNum = header.getRecordsCount();
        ReferencedEnvelope3D env = header.getDataEnvelope();
        File outLas = new File(outFile);
        try (ALasWriter writer = ALasWriter.getWriter(outLas, env.getCoordinateReferenceSystem())) {
            writer.setBounds(header);
            writer.open();
            pm.beginTask("Normalizing las...", (int) recordsNum);
            while (reader.hasNextPoint()) {
                LasRecord dot = reader.getNextPoint();
                double dtmValue = CoverageUtilities.getValue(dtm, dot.x, dot.y);
                if (HMConstants.isNovalue(dtmValue)) {
                    dtmValue = 0;
                }
                if (doAdd) {
                    dot.z += dtmValue;
                } else {
                    dot.z -= dtmValue;
                }
                writer.addPoint(dot);
                pm.worked(1);
            }
            pm.done();
        }
    }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) ALasWriter(org.hortonmachine.gears.io.las.core.ALasWriter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ReferencedEnvelope3D(org.geotools.geometry.jts.ReferencedEnvelope3D) ILasHeader(org.hortonmachine.gears.io.las.core.ILasHeader) File(java.io.File) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader) Execute(oms3.annotations.Execute)

Example 3 with ALasWriter

use of org.hortonmachine.gears.io.las.core.ALasWriter in project hortonmachine by TheHortonMachine.

the class LasMerger method process.

@Execute
public void process() throws Exception {
    checkNull(inFolder, outLas);
    CoordinateReferenceSystem crs = null;
    File inFolderFile = new File(inFolder);
    File[] lasList = inFolderFile.listFiles(new FilenameFilter() {

        public boolean accept(File arg0, String arg1) {
            return arg1.toLowerCase().endsWith(".las");
        }
    });
    StringBuilder sb = new StringBuilder("Merging files:");
    for (File file : lasList) {
        sb.append("\n").append(file.getAbsolutePath());
    }
    pm.message(sb.toString());
    // create readers and calculate bounds
    List<ALasReader> readers = new ArrayList<ALasReader>();
    double xMin = Double.POSITIVE_INFINITY;
    double yMin = Double.POSITIVE_INFINITY;
    double zMin = Double.POSITIVE_INFINITY;
    double xMax = Double.NEGATIVE_INFINITY;
    double yMax = Double.NEGATIVE_INFINITY;
    double zMax = Double.NEGATIVE_INFINITY;
    int count = 0;
    for (File lasFile : lasList) {
        ALasReader reader = ALasReader.getReader(lasFile, crs);
        reader.open();
        ILasHeader header = reader.getHeader();
        long recordsNum = header.getRecordsCount();
        count = (int) (count + recordsNum);
        ReferencedEnvelope3D envelope = header.getDataEnvelope();
        xMin = min(xMin, envelope.getMinX());
        yMin = min(yMin, envelope.getMinY());
        zMin = min(zMin, envelope.getMinZ());
        xMax = max(xMax, envelope.getMaxX());
        yMax = max(yMax, envelope.getMaxY());
        zMax = max(zMax, envelope.getMaxZ());
        readers.add(reader);
    }
    File outFile = new File(outLas);
    ALasWriter writer = ALasWriter.getWriter(outFile, crs);
    writer.setBounds(xMin, xMax, yMin, yMax, zMin, zMax);
    writer.open();
    pm.beginTask("Merging...", count);
    for (ALasReader reader : readers) {
        while (reader.hasNextPoint()) {
            LasRecord readNextLasDot = reader.getNextPoint();
            writer.addPoint(readNextLasDot);
            pm.worked(1);
        }
        reader.close();
    }
    writer.close();
    pm.done();
}
Also used : ArrayList(java.util.ArrayList) FilenameFilter(java.io.FilenameFilter) ALasWriter(org.hortonmachine.gears.io.las.core.ALasWriter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ReferencedEnvelope3D(org.geotools.geometry.jts.ReferencedEnvelope3D) ILasHeader(org.hortonmachine.gears.io.las.core.ILasHeader) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader) Execute(oms3.annotations.Execute)

Example 4 with ALasWriter

use of org.hortonmachine.gears.io.las.core.ALasWriter in project hortonmachine by TheHortonMachine.

the class LasSplitter method process.

@SuppressWarnings("unchecked")
@Execute
public void process() throws Exception {
    checkNull(inFile);
    File inLas = new File(inFile);
    String lasName = FileUtilities.getNameWithoutExtention(inLas);
    try (ALasReader reader = ALasReader.getReader(inLas, null)) {
        reader.open();
        ILasHeader header = reader.getHeader();
        long recordsNum = header.getRecordsCount();
        double[] xyzScale = header.getXYZScale();
        double[] xyzOffset = header.getXYZOffset();
        ReferencedEnvelope3D env = header.getDataEnvelope();
        double[] xRange = NumericsUtilities.range2Bins(env.getMinX(), env.getMaxX(), pCols);
        double[] yRange = NumericsUtilities.range2Bins(env.getMinY(), env.getMaxY(), pRows);
        STRtree envelopeTree = new STRtree();
        HashMap<Envelope, ALasWriter> env2LaswriterMap = new HashMap<>();
        int fileCount = 1;
        for (int x = 0; x < xRange.length - 1; x++) {
            double minX = xRange[x];
            double maxX = xRange[x + 1];
            for (int y = 0; y < yRange.length - 1; y++) {
                double minY = yRange[y];
                double maxY = yRange[y + 1];
                Envelope envelope = new Envelope(new Coordinate(minX, minY), new Coordinate(maxX, maxY));
                File outLasPiece = new File(inLas.getParentFile(), lasName + "_" + fileCount + ".las");
                ALasWriter writer = ALasWriter.getWriter(outLasPiece, env.getCoordinateReferenceSystem());
                writer.setOffset(xyzOffset[0], xyzOffset[1], xyzOffset[2]);
                writer.setScales(xyzScale[0], xyzScale[1], xyzScale[2]);
                writer.setBounds(minX, maxX, minY, maxY, env.getMinZ(), env.getMaxZ());
                writer.open();
                envelopeTree.insert(envelope, writer);
                env2LaswriterMap.put(envelope, writer);
                fileCount++;
            }
        }
        pm.beginTask("Split file...", (int) recordsNum);
        while (reader.hasNextPoint()) {
            LasRecord dot = reader.getNextPoint();
            Coordinate coord = new Coordinate(dot.x, dot.y);
            List<ALasWriter> result = envelopeTree.query(new Envelope(coord));
            int size = result.size();
            if (size == 0) {
                continue;
            }
            ALasWriter aLasWriter = result.get(0);
            aLasWriter.addPoint(dot);
            pm.worked(1);
        }
        pm.done();
        for (ALasWriter writer : env2LaswriterMap.values()) {
            writer.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) Envelope(org.locationtech.jts.geom.Envelope) ALasWriter(org.hortonmachine.gears.io.las.core.ALasWriter) Coordinate(org.locationtech.jts.geom.Coordinate) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ReferencedEnvelope3D(org.geotools.geometry.jts.ReferencedEnvelope3D) ILasHeader(org.hortonmachine.gears.io.las.core.ILasHeader) STRtree(org.locationtech.jts.index.strtree.STRtree) File(java.io.File) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader) Execute(oms3.annotations.Execute)

Example 5 with ALasWriter

use of org.hortonmachine.gears.io.las.core.ALasWriter in project hortonmachine by TheHortonMachine.

the class LasThresholder method process.

@Execute
public void process() throws Exception {
    checkNull(inLas, outLas);
    boolean doIntensity = false;
    if (pType.equals(LasUtils.INTENSITY)) {
        doIntensity = true;
    }
    CoordinateReferenceSystem crs = null;
    File lasFile = new File(inLas);
    File outFile = new File(outLas);
    try (// 
    ALasReader reader = ALasReader.getReader(lasFile, crs);
        ALasWriter writer = ALasWriter.getWriter(outFile, crs)) {
        reader.open();
        ILasHeader header = reader.getHeader();
        long recordsNum = header.getRecordsCount();
        writer.setBounds(header);
        writer.open();
        pm.beginTask("Filtering", (int) recordsNum);
        double min = Double.NEGATIVE_INFINITY;
        if (pLower != null) {
            min = pLower;
        }
        double max = Double.POSITIVE_INFINITY;
        if (pUpper != null) {
            max = pUpper;
        }
        while (reader.hasNextPoint()) {
            LasRecord readNextLasDot = reader.getNextPoint();
            double value = readNextLasDot.z;
            if (doIntensity) {
                value = readNextLasDot.intensity;
            }
            if (value < min) {
                pm.worked(1);
                continue;
            }
            if (value > max) {
                pm.worked(1);
                continue;
            }
            writer.addPoint(readNextLasDot);
            pm.worked(1);
        }
    }
    pm.done();
}
Also used : ALasWriter(org.hortonmachine.gears.io.las.core.ALasWriter) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ILasHeader(org.hortonmachine.gears.io.las.core.ILasHeader) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader) Execute(oms3.annotations.Execute)

Aggregations

File (java.io.File)15 ALasWriter (org.hortonmachine.gears.io.las.core.ALasWriter)15 LasRecord (org.hortonmachine.gears.io.las.core.LasRecord)15 ILasHeader (org.hortonmachine.gears.io.las.core.ILasHeader)13 ALasReader (org.hortonmachine.gears.io.las.core.ALasReader)12 Execute (oms3.annotations.Execute)11 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)9 ReferencedEnvelope3D (org.geotools.geometry.jts.ReferencedEnvelope3D)6 ArrayList (java.util.ArrayList)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Envelope (org.locationtech.jts.geom.Envelope)5 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)4 Geometry (org.locationtech.jts.geom.Geometry)4 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)3 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Entry (java.util.Map.Entry)2