import net.sf.marineapi.nmea.parser.SentenceFactory; import net.sf.marineapi.nmea.sentence.GGASentence; import net.sf.marineapi.nmea.sentence.SentenceValidator;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;
public class GPSBatchProcessor { private static final SentenceFactory factory = SentenceFactory.getInstance(); private static final int BATCH_SIZE = 10000; private static final int THREAD_COUNT = Runtime.getRuntime().availableProcessors(); public static void main(String[] args) throws Exception { String filePath = "path/to/your/nmea/data.txt"; processBatch(filePath); } public static void processBatch(String filePath) throws IOException, InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT); List<Future<?>> futures = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath), 8192)) { List<String> batch = new ArrayList<>(BATCH_SIZE); String line; long totalProcessed = 0; long startTime = System.currentTimeMillis(); while ((line = reader.readLine()) != null) { if (line.startsWith("$GPGGA") && SentenceValidator.isValid(line)) { batch.add(line); if (batch.size() >= BATCH_SIZE) { List<String> currentBatch = new ArrayList<>(batch); batch.clear(); futures.add(executor.submit(() -> processSentences(currentBatch))); totalProcessed += currentBatch.size(); if (totalProcessed % 1000000 == 0) { System.out.println("Processed " + totalProcessed + " sentences..."); } } } } if (!batch.isEmpty()) { futures.add(executor.submit(() -> processSentences(batch))); totalProcessed += batch.size(); } for (Future<?> future : futures) { future.get(); } long endTime = System.currentTimeMillis(); System.out.println("Completed processing " + totalProcessed + " sentences in " + (endTime - startTime) + "ms"); } catch (Exception e) { e.printStackTrace(); } finally { executor.shutdown(); } } private static void processSentences(List<String> sentences) { for (String sentence : sentences) { try { GGASentence gga = (GGASentence) factory.createParser(sentence); double latitude = gga.getPosition().getLatitude(); double longitude = gga.getPosition().getLongitude(); double altitude = gga.getAltitude(); int satellites = gga.getSatelliteCount(); } catch (Exception e) { System.err.println("Error processing: " + sentence); } } } private static void processGpsData(double lat, double lon, double alt, int satellites) { } }
|