📄 featureclass.java
字号:
} catch (IOException e) {
e.printStackTrace();
return 0;
}
return recordCount;
}
/*
* 读取属性字段信息
*/
private static Fields geteField(RandomAccessFile dbfFile, long oid)
{
Fields fields = new Fields();
List<Field> fieldList = new ArrayList<Field>();
//读取记录开始位置和字段数目
int firstPos = -1;
int recordLength = -1;
try {
dbfFile.seek(8);
int[] tInt = new int[1];
if(readInt(dbfFile, 2, tInt))
firstPos = tInt[0] + 1;
dbfFile.seek(10);
if(readInt(dbfFile, 2, tInt))
recordLength = tInt[0];
int fieldCount = (firstPos - 32 - 1) / 32;
//读取字段信息
int offset = 0;
for(int i = 0; i < fieldCount;i++){
Field field = new Field();
dbfFile.seek(32 + i * 32);
byte[] buffer10 = new byte[10];
dbfFile.read(buffer10);
field.setName(new String(buffer10).trim());
dbfFile.seek(32 + i * 32 + 10 + 1);
readInt(dbfFile, 4, tInt);
field.setType(getFieldType(tInt[0]));
dbfFile.seek(32 + i * 32 + 10 + 1 + 1 + 4);
readInt(dbfFile, 1, tInt);
field.setLength(tInt[0]);
dbfFile.seek(32 + i * 32 + 10 + 1 + 1 + 4 + 1);
readInt(dbfFile, 2, tInt);
field.setScale(tInt[0]);
byte[] bufferValue = new byte[field.getLength()];
dbfFile.seek(firstPos + oid * recordLength + offset);
dbfFile.read(bufferValue);
field.setValue(new String(bufferValue));
offset += field.getLength();
fieldList.add(field);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
fields.setFields(fieldList);
return fields;
}
/*
* 读取shp文件中的几何对象
*/
private static Geometry getGeometry(RandomAccessFile shpFile,
RandomAccessFile shxFile ,GeometryType type, long oid)
{
if(type.equals(GeometryType.POINT))
return getPoint(shpFile, oid);
if(type.equals(GeometryType.POLYLINE))
return getLine(shpFile, shxFile, oid);
return null;
}
/*
* 读取点对象
*/
private static Geometry getPoint(RandomAccessFile shpFile, long oid)
{
Point pnt = new Point();
try {
shpFile.seek(100 + oid * 28 + 12);
double[] tDouble = new double[1];
if(!readDouble(shpFile, tDouble))
return null;
pnt.setX(tDouble[0]);
if(!readDouble(shpFile, tDouble))
return null;
pnt.setY(tDouble[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return pnt;
}
/*
* 取oid线对象
*/
private static Geometry getLine(RandomAccessFile shpFile, RandomAccessFile
shxFile, long oid) {
//读shx文件,取第oid个polyline在shp文件的偏移量
long offset = queryOffset(shxFile, oid);
try {
shpFile.seek(offset);
} catch (IOException e) {
e.printStackTrace();
return null;
}
double[] tDouble = new double[1];
int[] tInt = new int[1];
Polyline line = new Polyline();
//读Polyline的Envelope
readDouble(shpFile, tDouble);
double minx = tDouble[0];
readDouble(shpFile, tDouble);
double miny = tDouble[0];
readDouble(shpFile, tDouble);
double maxx = tDouble[0];
readDouble(shpFile, tDouble);
double maxy = tDouble[0];
Envelope enve = new Envelope(maxx,maxy,minx,miny);
line.setEnvelope(enve);
//读partNum和pointNum
readInt(shpFile, 4, tInt);
line.setNumParts(tInt[0]);
readInt(shpFile, 4, tInt);
line.setNumPoints(tInt[0]);
//读每个segment起始点的索引号
int[] parts = new int[line.getNumParts()];
for(int i = 0;i < line.getNumParts();i++){
if(!readInt(shpFile, 4, tInt))
break;
parts[i] = tInt[0];
}
line.setParts(parts);
//读Polyline节点
Point[] points = new Point[line.getNumPoints()];
for(int i = 0;i < line.getNumPoints();i++){
Point pnt = new Point();
if(!readDouble(shpFile, tDouble))
break;
pnt.setX(tDouble[0]);
if(!readDouble(shpFile, tDouble))
break;
pnt.setY(tDouble[0]);
points[i] = pnt;
}
line.setPoints(points);
return line;
}
/*
* 查第oid个geometry在shp中偏移量
*/
private static long queryOffset(RandomAccessFile shxFile, long oid)
{
long offset = 0;
try {
shxFile.seek(100 + oid * 8);
byte[] buffer = new byte[4];
shxFile.read(buffer);
ByteBuffer bf = ByteBuffer.wrap(buffer);
offset = bf.getInt();
} catch (IOException e) {
e.printStackTrace();
}
return offset * 2 + 12;
}
/*
* 读取一个整数,如果文件已经到结尾了,返回false,否则返回true
*/
private static boolean readInt(RandomAccessFile shpFile, int length,
int[] out)
{
byte[] buffer = new byte[length];
try {
int count = shpFile.read(buffer);
if(count > 0){
ByteBuffer bf = ByteBuffer.wrap(converse(buffer, 4));
out[0] = bf.getInt();
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/*
* 读取一个Double型浮点数,成功返回true,否则返回false
*/
private static boolean readDouble(RandomAccessFile shpFile,double[] out)
{
byte[] buffer = new byte[8];
try {
int count = shpFile.read(buffer);
if(count > 0){
ByteBuffer bf = ByteBuffer.wrap(converse(buffer, 8));
out[0] = bf.getDouble();
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private static byte[] converse(byte[] in, int length){
byte[] out = new byte[length];
for(int i = 0;i < in.length;i++){
out[length - i - 1] = in[i];
}
return out;
}
private static FieldType getFieldType(int code){
if(code == 78)
return FieldType.NUMBER;
if(code == 67)
return FieldType.STRING;
return null;
}
/**
*
* @param type 字段名称
* @param value 字段值
* @return 要素列表
* @
*/
public List<Feature> get(String type,String value) {
List<Feature> list = new ArrayList<Feature>();
for(int i = 0;i < featureCount;i++){
Feature feature = get(i);
if(feature.getValue(type).toString().trim().equals(value))
list.add(feature);
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -