ShpCount.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.ruoyi.test;
  2. import java.io.*;
  3. public class ShpCount {
  4. public static void main(String[] args) throws Exception {
  5. String shpPath = "d:\\Web\\PlatformModel\\ruoyi-admin\\uploads\\models\\geojson\\8351f649bb8443f8bf3adc0d34f89a62\\流域.shp";
  6. File shpFile = new File(shpPath);
  7. System.out.println("SHP File Size: " + shpFile.length() + " bytes");
  8. FileInputStream fis = new FileInputStream(shpFile);
  9. fis.skip(100);
  10. byte[] recordHeader = new byte[8];
  11. int polygonCount = 0;
  12. int polylineCount = 0;
  13. int pointCount = 0;
  14. int totalRecords = 0;
  15. while (fis.available() > 8) {
  16. if (fis.read(recordHeader) < 8) break;
  17. int contentLengthWords = readIntBE(recordHeader, 4);
  18. int contentLength = contentLengthWords * 2;
  19. byte[] recordContent = new byte[contentLength];
  20. int bytesRead = fis.read(recordContent);
  21. if (bytesRead < contentLength) break;
  22. int recShapeType = recordContent[0] & 0xFF;
  23. totalRecords++;
  24. if (recShapeType == 5 || recShapeType == 15) {
  25. polygonCount++;
  26. } else if (recShapeType == 3 || recShapeType == 13) {
  27. polylineCount++;
  28. } else if (recShapeType == 1 || recShapeType == 11) {
  29. pointCount++;
  30. }
  31. }
  32. fis.close();
  33. System.out.println("Total Records: " + totalRecords);
  34. System.out.println("Polygon Count: " + polygonCount);
  35. System.out.println("Polyline Count: " + polylineCount);
  36. System.out.println("Point Count: " + pointCount);
  37. }
  38. static int readIntBE(byte[] data, int offset) {
  39. return ((data[offset] & 0xFF) << 24) |
  40. ((data[offset + 1] & 0xFF) << 16) |
  41. ((data[offset + 2] & 0xFF) << 8) |
  42. (data[offset + 3] & 0xFF);
  43. }
  44. }