| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package com.ruoyi.test;
- import java.io.*;
- import java.nio.*;
- import java.util.*;
- public class ShpTest {
- public static void main(String[] args) throws Exception {
- String shpPath = "d:\\Web\\PlatformModel\\ruoyi-admin\\uploads\\models\\geojson\\8aa64945328e428d97923165c8b8cf31\\流域.shp";
-
- File shpFile = new File(shpPath);
- System.out.println("SHP File: " + shpPath);
- System.out.println("SHP File Size: " + shpFile.length() + " bytes");
-
- FileInputStream fis = new FileInputStream(shpFile);
- byte[] header = new byte[100];
- fis.read(header);
-
- int fileCode = readIntBE(header, 0);
- System.out.println("File Code: " + fileCode + " (expected 9994)");
- System.out.println("Version: " + readIntLE(header, 28));
- int shapeType = header[32] & 0xFF;
- System.out.println("Shape Type: " + shapeType);
-
- System.out.println("\n--- Reading Records ---");
-
- int recordCount = 0;
- byte[] recordHeader = new byte[8];
-
- while (fis.available() > 8 && recordCount < 10) {
- if (fis.read(recordHeader) < 8) break;
-
- int recordNumber = readIntBE(recordHeader, 0);
- int contentLengthWords = readIntBE(recordHeader, 4);
- int contentLength = contentLengthWords * 2;
-
- System.out.println("\nRecord #" + (recordCount + 1) + ":");
- System.out.println(" Content Length: " + contentLength + " bytes");
-
- byte[] recordContent = new byte[contentLength];
- int bytesRead = fis.read(recordContent);
- if (bytesRead < contentLength) break;
-
- int recShapeType = recordContent[0] & 0xFF;
- System.out.println(" Record Shape Type: " + recShapeType);
-
- if (recShapeType == 1) {
- double x = readDoubleLE(recordContent, 4);
- double y = readDoubleLE(recordContent, 12);
- System.out.println(" Point coords: x=" + x + ", y=" + y);
- } else if (recShapeType == 3) {
- int numParts = readIntLE(recordContent, 32);
- int numPoints = readIntLE(recordContent, 36);
- System.out.println(" Polyline - numParts: " + numParts + ", numPoints: " + numPoints);
- } else if (recShapeType == 5) {
- int numParts = readIntLE(recordContent, 32);
- int numPoints = readIntLE(recordContent, 36);
- System.out.println(" Polygon - numParts: " + numParts + ", numPoints: " + numPoints);
- } else if (recShapeType == 13) {
- int numParts = readIntLE(recordContent, 32);
- int numPoints = readIntLE(recordContent, 36);
- System.out.println(" PolylineZ - numParts: " + numParts + ", numPoints: " + numPoints);
- } else if (recShapeType == 15) {
- int numParts = readIntLE(recordContent, 32);
- int numPoints = readIntLE(recordContent, 36);
- System.out.println(" PolygonZ - numParts: " + numParts + ", numPoints: " + numPoints);
- } else {
- System.out.println(" UNSUPPORTED TYPE!");
- }
-
- recordCount++;
- }
-
- fis.close();
- System.out.println("\nTotal records read: " + recordCount);
- }
-
- static int readIntBE(byte[] data, int offset) {
- return ((data[offset] & 0xFF) << 24) |
- ((data[offset + 1] & 0xFF) << 16) |
- ((data[offset + 2] & 0xFF) << 8) |
- (data[offset + 3] & 0xFF);
- }
-
- static int readIntLE(byte[] data, int offset) {
- return (data[offset] & 0xFF) |
- ((data[offset + 1] & 0xFF) << 8) |
- ((data[offset + 2] & 0xFF) << 16) |
- ((data[offset + 3] & 0xFF) << 24);
- }
-
- static double readDoubleLE(byte[] data, int offset) {
- long bits = readLongLE(data, offset);
- return Double.longBitsToDouble(bits);
- }
-
- static long readLongLE(byte[] data, int offset) {
- return (data[offset] & 0xFFL) |
- ((data[offset + 1] & 0xFFL) << 8) |
- ((data[offset + 2] & 0xFFL) << 16) |
- ((data[offset + 3] & 0xFFL) << 24) |
- ((data[offset + 4] & 0xFFL) << 32) |
- ((data[offset + 5] & 0xFFL) << 40) |
- ((data[offset + 6] & 0xFFL) << 48) |
- ((data[offset + 7] & 0xFFL) << 56);
- }
- }
|