ShpTest.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.ruoyi.test;
  2. import java.io.*;
  3. import java.nio.*;
  4. import java.util.*;
  5. public class ShpTest {
  6. public static void main(String[] args) throws Exception {
  7. String shpPath = "d:\\Web\\PlatformModel\\ruoyi-admin\\uploads\\models\\geojson\\8aa64945328e428d97923165c8b8cf31\\流域.shp";
  8. File shpFile = new File(shpPath);
  9. System.out.println("SHP File: " + shpPath);
  10. System.out.println("SHP File Size: " + shpFile.length() + " bytes");
  11. FileInputStream fis = new FileInputStream(shpFile);
  12. byte[] header = new byte[100];
  13. fis.read(header);
  14. int fileCode = readIntBE(header, 0);
  15. System.out.println("File Code: " + fileCode + " (expected 9994)");
  16. System.out.println("Version: " + readIntLE(header, 28));
  17. int shapeType = header[32] & 0xFF;
  18. System.out.println("Shape Type: " + shapeType);
  19. System.out.println("\n--- Reading Records ---");
  20. int recordCount = 0;
  21. byte[] recordHeader = new byte[8];
  22. while (fis.available() > 8 && recordCount < 10) {
  23. if (fis.read(recordHeader) < 8) break;
  24. int recordNumber = readIntBE(recordHeader, 0);
  25. int contentLengthWords = readIntBE(recordHeader, 4);
  26. int contentLength = contentLengthWords * 2;
  27. System.out.println("\nRecord #" + (recordCount + 1) + ":");
  28. System.out.println(" Content Length: " + contentLength + " bytes");
  29. byte[] recordContent = new byte[contentLength];
  30. int bytesRead = fis.read(recordContent);
  31. if (bytesRead < contentLength) break;
  32. int recShapeType = recordContent[0] & 0xFF;
  33. System.out.println(" Record Shape Type: " + recShapeType);
  34. if (recShapeType == 1) {
  35. double x = readDoubleLE(recordContent, 4);
  36. double y = readDoubleLE(recordContent, 12);
  37. System.out.println(" Point coords: x=" + x + ", y=" + y);
  38. } else if (recShapeType == 3) {
  39. int numParts = readIntLE(recordContent, 32);
  40. int numPoints = readIntLE(recordContent, 36);
  41. System.out.println(" Polyline - numParts: " + numParts + ", numPoints: " + numPoints);
  42. } else if (recShapeType == 5) {
  43. int numParts = readIntLE(recordContent, 32);
  44. int numPoints = readIntLE(recordContent, 36);
  45. System.out.println(" Polygon - numParts: " + numParts + ", numPoints: " + numPoints);
  46. } else if (recShapeType == 13) {
  47. int numParts = readIntLE(recordContent, 32);
  48. int numPoints = readIntLE(recordContent, 36);
  49. System.out.println(" PolylineZ - numParts: " + numParts + ", numPoints: " + numPoints);
  50. } else if (recShapeType == 15) {
  51. int numParts = readIntLE(recordContent, 32);
  52. int numPoints = readIntLE(recordContent, 36);
  53. System.out.println(" PolygonZ - numParts: " + numParts + ", numPoints: " + numPoints);
  54. } else {
  55. System.out.println(" UNSUPPORTED TYPE!");
  56. }
  57. recordCount++;
  58. }
  59. fis.close();
  60. System.out.println("\nTotal records read: " + recordCount);
  61. }
  62. static int readIntBE(byte[] data, int offset) {
  63. return ((data[offset] & 0xFF) << 24) |
  64. ((data[offset + 1] & 0xFF) << 16) |
  65. ((data[offset + 2] & 0xFF) << 8) |
  66. (data[offset + 3] & 0xFF);
  67. }
  68. static int readIntLE(byte[] data, int offset) {
  69. return (data[offset] & 0xFF) |
  70. ((data[offset + 1] & 0xFF) << 8) |
  71. ((data[offset + 2] & 0xFF) << 16) |
  72. ((data[offset + 3] & 0xFF) << 24);
  73. }
  74. static double readDoubleLE(byte[] data, int offset) {
  75. long bits = readLongLE(data, offset);
  76. return Double.longBitsToDouble(bits);
  77. }
  78. static long readLongLE(byte[] data, int offset) {
  79. return (data[offset] & 0xFFL) |
  80. ((data[offset + 1] & 0xFFL) << 8) |
  81. ((data[offset + 2] & 0xFFL) << 16) |
  82. ((data[offset + 3] & 0xFFL) << 24) |
  83. ((data[offset + 4] & 0xFFL) << 32) |
  84. ((data[offset + 5] & 0xFFL) << 40) |
  85. ((data[offset + 6] & 0xFFL) << 48) |
  86. ((data[offset + 7] & 0xFFL) << 56);
  87. }
  88. }