Java 读Shapefile

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Java 读Shapefile
package data;
import geometry.Point;
import geometry.Polyline;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import util.Convert;
public class PolylineShape {
@SuppressWarnings("unused")
private final int type = 3;
private String fileName;
private ArrayList<Polyline> list;
private double xMin;
private double yMin;
private double xMax;
private double yMax;
private byte[] read4(FileInputStream input) { byte[] b = new byte[4];
for(int i = 0; i < 4; i++) {
try {
b[i] = (byte)input.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
private int read4Big(FileInputStream input) { return Convert.bytesToIntBig(read4(input));
}
private int read4Little(FileInputStream input) { return Convert.bytesToIntLittle(read4(input));
}
private double read8(FileInputStream input) { byte[] b = new byte[8];
for(int i = 0; i < 8; i++) {
try {
b[i] = (byte)input.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Convert.bytesToDouble(b);
}
private void read() throws Exception
{
File file = new File(fileName);
FileInputStream input = new FileInputStream(file);
int index = 0;
while(input.available() > 0) {
input.read();
index++;
if(index == 36)
break;
}
xMin = this.read8(input);
yMin = this.read8(input);
xMax = this.read8(input);
yMax = this.read8(input);
index += 32;
while(input.available() > 0) {
input.read();
index++;
if(index == 100)
break;
}
while(input.available() > 0) {
list.add(this.readPolyline(input));
}
System.out.println(input.available());
}
private Polyline readPolyline(FileInputStream input) { int id = this.read4Big(input);
read4Big(input);//记录长度
read4Little(input);//几何类型
double[] box = new double[4];
box[0] = read8(input);
box[1] = read8(input);
box[2] = read8(input);
box[3] = read8(input);
int numParts = read4Little(input);
int numPoints = read4Little(input);
int[] parts = new int[numParts];
for(int i = 0; i < numParts; i++)
parts[i] = read4Little(input);
Point[] points = new Point[numPoints];
for(int i = 0; i < numPoints; i++) {
points[i] = new Point();
points[i].setX(read8(input));
points[i].setY(read8(input));
}
Polyline line = new Polyline(id, box, parts, points);
return line;
// TODO Auto-generated method stub
}
PolylineShape(String fileName) throws Exception { this.fileName = fileName;
list = new ArrayList<Polyline>();
read();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub
new PolylineShape("complexshp.shp");
}
}。

相关文档
最新文档