Search in sources :

Example 1 with ALasReader

use of org.hortonmachine.gears.io.las.core.ALasReader 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 ALasReader

use of org.hortonmachine.gears.io.las.core.ALasReader 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 ALasReader

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

the class LasShapeVectorizer method process.

@Execute
public void process() throws Exception {
    checkNull(inLas);
    CoordinateReferenceSystem crs = null;
    double west = Double.POSITIVE_INFINITY;
    double south = Double.POSITIVE_INFINITY;
    double east = Double.NEGATIVE_INFINITY;
    double north = Double.NEGATIVE_INFINITY;
    // check the real minimum bounds
    pm.beginTask("Reading real bounds...", IHMProgressMonitor.UNKNOWN);
    long recordsCount = 0;
    try (ALasReader reader = ALasReader.getReader(new File(inLas), null)) {
        reader.open();
        ILasHeader header = reader.getHeader();
        crs = header.getCrs();
        recordsCount = header.getRecordsCount();
        while (reader.hasNextPoint()) {
            LasRecord dot = reader.getNextPoint();
            west = Math.min(west, dot.x);
            south = Math.min(south, dot.y);
            east = Math.max(east, dot.x);
            north = Math.max(north, dot.y);
        }
    }
    pm.done();
    // buffer by one resolution
    north = north + pYres;
    south = south - pYres;
    west = west - pXres;
    east = east + pXres;
    int rows = (int) round((north - south) / pYres);
    int cols = (int) round((east - west) / pXres);
    // bounds snapped on grid
    east = west + cols * pXres;
    north = south + rows * pYres;
    final GridGeometry2D gridGeometry2D = CoverageUtilities.gridGeometryFromRegionValues(north, south, east, west, cols, rows, crs);
    RegionMap regionMap = CoverageUtilities.gridGeometry2RegionParamsMap(gridGeometry2D);
    final WritableRaster wr = CoverageUtilities.createWritableRaster(cols, rows, null, null, HMConstants.doubleNovalue);
    // now map the points on the raster
    pm.beginTask("Mapping points on raster...", (int) recordsCount);
    try (ALasReader reader = ALasReader.getReader(new File(inLas), null)) {
        reader.open();
        final Point gridPoint = new Point();
        while (reader.hasNextPoint()) {
            LasRecord dot = reader.getNextPoint();
            double dotZ = dot.z;
            Coordinate coordinate = new Coordinate(dot.x, dot.y, dotZ);
            CoverageUtilities.colRowFromCoordinate(coordinate, gridGeometry2D, gridPoint);
            wr.setSample(gridPoint.x, gridPoint.y, 0, 1);
            pm.worked(1);
        }
    }
    pm.done();
    GridCoverage2D gridCoverage2D = CoverageUtilities.buildCoverage("mapped", wr, regionMap, crs);
    OmsVectorizer vectorizer = new OmsVectorizer();
    vectorizer.inRaster = gridCoverage2D;
    vectorizer.pm = pm;
    vectorizer.process();
    SimpleFeatureCollection outVector = vectorizer.outVector;
    dumpVector(outVector, outShp);
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) RegionMap(org.hortonmachine.gears.utils.RegionMap) Point(java.awt.Point) Point(java.awt.Point) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) Coordinate(org.locationtech.jts.geom.Coordinate) OmsVectorizer(org.hortonmachine.gears.modules.v.vectorize.OmsVectorizer) WritableRaster(java.awt.image.WritableRaster) 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 ALasReader

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

the class OmsPointCloudMaximaFinderStream method main.

public static void main(String[] args) throws Exception {
    String lasPath = "/media/hydrologis/LESTOPLUS/test_lidar_spatialite/las_aurina/uni_bz_44.las";
    String outPath = "/home/hydrologis/TMP/tops_2m.asc";
    OmsPointCloudMaximaFinderStream s = new OmsPointCloudMaximaFinderStream();
    List<LasRecord> dotList = new ArrayList<>();
    try (ALasReader reader = ALasReader.getReader(new File(lasPath), null)) {
        reader.open();
        while (reader.hasNextPoint()) {
            LasRecord dot = (LasRecord) reader.getNextPoint();
            dotList.add(dot);
        }
        s.inLas = dotList;
        ReferencedEnvelope3D dataEnvelope = reader.getHeader().getDataEnvelope();
        s.aoi = new ReferencedEnvelope(dataEnvelope);
        s.pBaseGridResolution = 0.5;
        s.pMaxRadius = 2;
        s.process();
        OmsRasterWriter.writeRaster(outPath, s.outCoverage);
    }
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) LasRecord(org.hortonmachine.gears.io.las.core.LasRecord) ReferencedEnvelope3D(org.geotools.geometry.jts.ReferencedEnvelope3D) ArrayList(java.util.ArrayList) File(java.io.File) ALasReader(org.hortonmachine.gears.io.las.core.ALasReader)

Example 5 with ALasReader

use of org.hortonmachine.gears.io.las.core.ALasReader 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)

Aggregations

ALasReader (org.hortonmachine.gears.io.las.core.ALasReader)24 File (java.io.File)22 ILasHeader (org.hortonmachine.gears.io.las.core.ILasHeader)20 LasRecord (org.hortonmachine.gears.io.las.core.LasRecord)19 Execute (oms3.annotations.Execute)16 ReferencedEnvelope3D (org.geotools.geometry.jts.ReferencedEnvelope3D)14 ALasWriter (org.hortonmachine.gears.io.las.core.ALasWriter)11 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)11 ArrayList (java.util.ArrayList)9 ModelsIllegalargumentException (org.hortonmachine.gears.libs.exceptions.ModelsIllegalargumentException)7 Coordinate (org.locationtech.jts.geom.Coordinate)6 Polygon (org.locationtech.jts.geom.Polygon)6 Envelope (org.locationtech.jts.geom.Envelope)5 SimpleFeature (org.opengis.feature.simple.SimpleFeature)5 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)5 FilenameFilter (java.io.FilenameFilter)4 STRtreeJGT (org.hortonmachine.gears.io.las.index.strtree.STRtreeJGT)4 FileFilter (java.io.FileFilter)3 List (java.util.List)3 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)3