搜档网
当前位置:搜档网 › [JAVA]读取ShapeFile文件的源代码

[JAVA]读取ShapeFile文件的源代码

[JAVA]读取ShapeFile文件的源代码
时间:2004-9-6 23:58:46 来源:大地测量学基础 阅读437次

import java.awt.Color;
import java.io.*;
import https://www.sodocs.net/doc/ea5536170.html,.URL;
import https://www.sodocs.net/doc/ea5536170.html,.URLConnection;
import java.util.zip.*;
import https://www.sodocs.net/doc/ea5536170.html,g.dbffile.DbfFileException;
import https://www.sodocs.net/doc/ea5536170.html,g.dbffile.DbfNet;
import https://www.sodocs.net/doc/ea5536170.html,g.shapefile.*;

public class ShapefileReader
{
public Shapefile sf;
public DbfNet dbf;
private int idCol;
private String name;

public ShapefileReader(String baseFilename)
{
super();
sf = null;
dbf = null;
idCol = 3;
name = "none";
name = baseFilename;
try
{
sf = new Shapefile(baseFilename + ".shp");
}
catch(Exception e)
{
System.out.println(e);
}
}

public ShapefileReader(URL base, int idCol)
{
super();
sf = null;
dbf = null;
this.idCol = 3;
name = "none";
this.idCol = idCol;
name = base.getFile();
String sub = "";
if(name.indexOf(63) >= 0)
sub = name.substring(name.indexOf(63), https://www.sodocs.net/doc/ea5536170.html,stIndexOf(47));
System.out.println("Sub " + sub);
boolean shpZip = false;
boolean dbfZip = false;
try
{
System.out.println("SFR: Looking for .zip version of " + name);
String ext = ".zip";
String noExt = base.getFile();
System.out.println("No Ext " + noExt);
if(noExt.toLowerCase().endsWith(".shp") noExt.toLowerCase().endsWith(".zip"))
noExt = name.substring(0, name.length() - 4);
System.out.println("No Ext " + noExt);
URL zipURL = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext);
System.out.println("zip url = " + zipURL);
URLConnection uc = zipURL.openConnection();
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
System.out.println("Opening zis");
int size = uc.getContentLength();
if(size != -1)
{
byte buf1[] = new byte[size];
byte got[] = new byte[size / 10];
InputStream is = uc.getInputStream();
int grab = 0;
for(int total = 0; (grab = is.read(got)) > -1; total += grab)
System.arraycopy(got, 0, buf1, total, grab);

got = null;
ByteArrayInputStream zipfile = new ByteArrayInputStream(buf1);
ZipInputStream zis = new ZipInputStream(zipfile);
System.out.println("Looking for entries");
ZipEntry ze;
while((ze = zis.getNextEntry()) != null && (!shpZip !dbfZip))
{
System.out.println("Found entry");
System.out.println(ze);
System.out.println("Entry: " + ze.getName());
System.out.println("Getting entry");
if(ze.getName().toLowerCase().endsWith(".shp"))
{
name = ze.getName();
System.out.println("Found .shp in zip file");
size = (int)ze.getSize();
byte buf[] = new byte[size];
byte in[] = new byte[size];
int n = 0;
for(int index = 0; (n = zis.read(in)) > -1; index += n)
System.arraycopy(in, 0, buf, index, n);

zis.closeEntry();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
sf = new Shapefile(bais);
shpZip = true;
}
else
if(ze.getName().toLowerCase().endsWith(".dbf"))
{
System.out.println("Found .dbf in zip file");
size = (int)ze.getSize();
byte buf[] = new byte[size];
byte in[] = new byte[siz

e];
int n;
for(int index = 0; (n = zis.read(in)) > -1; index += n)
System.arraycopy(in, 0, buf, index, n);

zis.closeEntry();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
dbf = new DbfNet(bais);
dbfZip = true;
}
}

if(shpZip && dbfZip)
{
System.out.println("Zip file version used " + name);
return;
}
}
}
catch(ZipException ze)
{
System.err.println("SFR: zip read failed " + ze);
}
catch(IOException _ex)
{
System.err.println("SFR: zip read not possible, looking for .shp and .dbf ");
}
catch(Exception _ex)
{
System.err.println("SFR: General exception in zip read ");
}
System.out.println("Looking for files outside of zip file");
try
{
String ext = ".shp";
String noExt = base.getFile();
if(noExt.toLowerCase().endsWith(".shp") noExt.toLowerCase().endsWith(".zip"))
noExt = name.substring(0, name.length() - 4);
URL shapeURL = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext);
if(!shpZip)
sf = new Shapefile(shapeURL);
ext = ".dbf";
URL dbfURL = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext);
if(!dbfZip)
dbf = new DbfNet(dbfURL);
URL shxURL = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext);
sf.readIndex(shxURL.openStream());
}
catch(Exception e)
{
System.err.println(e);
}
}

public ShapefileReader(InputStream shpIn, InputStream dbfIn, int idCol)
{
super();
sf = null;
dbf = null;
this.idCol = 3;
name = "none";
this.idCol = idCol;
try
{
sf = new Shapefile(shpIn);
if(dbfIn != null)
dbf = new DbfNet(dbfIn);
}
catch(Exception e)
{
System.err.println(e);
}
}

public ShapefileReader(InputStream shpIn, InputStream dbfIn)
{
super();
sf = null;
dbf = null;
idCol = 3;
name = "none";
try
{
sf = new Shapefile(shpIn);
if(dbfIn != null)
dbf = new DbfNet(dbfIn);
}
catch(Exception e)
{
System.err.println(e);
}
guessIdCol();
}

public ShapefileReader(URL base)
{
this(base, 0);
guessIdCol();
}

private void guessIdCol()
{
switch(sf.getShapeType())
{
case 5: // '\005'
idCol = 2;
break;

case 23: // '\027'
idCol = 5;
break;

case 3: // '\003'
idCol = 5;
break;

case 1: // '\001'
idCol = 0;
break;

}
if(idCol > dbf.getNumFields() - 1)
idCol = 0;
System.out.println("ID col guessed at " + idCol + " " + dbf.getNumFields());
}

public void setIdCol(int col)
{
idCol = col;
}

public void setIdCol(String name)
{
int col = dbf.getFieldNumber(name);
idCol = col >= 0 ? col : idCol;
}

public int getShapeType()
{
return sf.getShapeType();
}

public Theme getTheme()
{
Layer l = null;
switch(getShapeType())
{
case 5: // '\005'
l = readPolygons();
break;

case 3: // '\003'
l = readLines();
break;

case 1: // '\001'
l = readPoints();
break;

case 23: // '\027'
l = readLinesM();
break;

default:
return null;

}
Theme t = new Theme(l);
t.setName(name);
return t;
}

public Theme getTheme(Shader shade, String colName)
{
Theme t = getTheme(

);
if(colName != null)
{
int col = dbf.getFieldNumber(colName);
if(col >= 0)
{
GeoData d = readData(col);
shade.setRange(d);
t.setShader(shade);
t.setGeoData(d);
}
else
{
System.err.println("Column " + colName + " not found in .dbf file");
}
}
return t;
}

public Theme getTheme(String colName, Color c)
{
return getTheme(new satShader(c), colName);
}

public LineLayer readLines()
{
LineLayer map = new LineLayer();
https://www.sodocs.net/doc/ea5536170.html,g.shapefile.ShapefileShape polys[] = sf.getShapes();
ShapePoint p[] = null;
int count = sf.getRecordCount();
for(int i = 0; i < count; i++)
{
ShapeArc poly = (ShapeArc)polys[i];
int nPoints = poly.getNumPoints();
int nParts = poly.getNumParts();
for(int j = 0; j < nParts; j++)
{
p = poly.getPartPoints(j);
GeoPolygon gp = new GeoPolygon(i + 1, p);
map.addGeoPolygon(gp);
}

}

return map;
}

public LineLayer readLinesM()
{
LineLayer map = new LineLayer();
https://www.sodocs.net/doc/ea5536170.html,g.shapefile.ShapefileShape polys[] = sf.getShapes();
ShapePoint p[] = null;
int count = sf.getRecordCount();
for(int i = 0; i < count; i++)
{
ShapeArcM poly = (ShapeArcM)polys[i];
int nPoints = poly.getNumPoints();
int nParts = poly.getNumParts();
for(int j = 0; j < nParts; j++)
{
p = poly.getPartPoints(j);
double px[] = new double[p.length];
double py[] = new double[p.length];
for(int k = 0; k < p.length; k++)
{
px[k] = p[k].getX();
py[k] = p[k].getY();
}

GeoPolygon gp = new GeoPolygon(i + 1, 0.0D, 0.0D, px, py, p.length);
map.addGeoPolygon(gp);
}

}

return map;
}

public GeoData[] readData()
{
GeoData data[] = new SimpleGeoData[0];
if(dbf != null)
try
{
int ids[] = getIds();
data = new SimpleGeoData[dbf.getNumFields()];
for(int j = 0; j < dbf.getNumFields(); j++)
data[j] = readData(j, ids);

}
catch(Exception e)
{
System.err.println("ShapeFileReader error :" + e);
}
return data;
}

private int[] getIds()
throws IOException, DbfFileException
{
int ids[];
if(dbf.getFieldType(idCol) == 'N')
{
Float fids[] = dbf.getFloatCol(idCol);
ids = new int[fids.length];
for(int i = 0; i < fids.length; i++)
ids[i] = fids[i].intvalue();

return ids;
}
System.err.println("No ID column found, using sequential ids");
ids = new int[dbf.getLastRec()];
for(int i = 0; i < dbf.getLastRec(); i++)
ids[i] = i + 1;

return ids;
}

public GeoData readData(int col)
{
try
{
return readData(col, getIds());
}
catch(Exception e)
{
System.err.println("ShapeFileReader error :" + e);
}
return null;
}

public GeoData readData(int col, int ids[])
{
SimpleGeoData data = null;
try
{
data = new SimpleGeoData();
data.setName(dbf.getFieldName(col).toString());
if(dbf.getFieldType(col) != 'N')
{
String s[] = dbf.getStringCol(col);
for(int k = 0; k < s.length; k++)
data.setText(ids[k], s[k]);

}
else
{
Float f[] = dbf.getFloatCol(col);
double missing = data.getMissingvalueCode();
for(int k = 0; k < f.length; k++)
if(f[k] != null)
data.setvalue(ids[k], f[k

].doublevalue());
else
data.setvalue(ids[k], missing);

}
}
catch(Exception e)
{
System.err.println("ShapeFileReader error :" + e);
}
return data;
}

public GeoData readData(String colName)
{
if(colName != null)
{
int col = dbf.getFieldNumber(colName);
if(col >= 0)
return readData(col);
System.err.println("Column " + colName + " could not be found");
}
return null;
}

public PolygonLayer readPolygons()
{
boolean gotIds = false;
Float id[] = null;
if(dbf != null)
try
{
System.out.println("I think that col " + dbf.getFieldName(idCol) + " is the system id col");
id = dbf.getFloatCol(idCol);
gotIds = true;
}
catch(IOException e)
{
System.err.println("ID's could not be read from .dbf file " + e);
}
catch(DbfFileException e)
{
System.err.println("ID's could not be read from .dbf file " + e);
}
if(!gotIds)
System.out.println("Using sequential ids");
PolygonLayer map = new PolygonLayer();
https://www.sodocs.net/doc/ea5536170.html,g.shapefile.ShapefileShape polys[] = sf.getShapes();
ShapePoint p[] = null;
int count = sf.getRecordCount();
for(int i = 0; i < count; i++)
{
ShapePolygon poly = (ShapePolygon)polys[i];
int nPoints = poly.getNumPoints();
int nParts = poly.getNumParts();
p = poly.getPartPoints(0);
GeoPolygon gp = new GeoPolygon(gotIds ? id[i].intvalue() : i + 1, p);
if(nParts > 1)
{
System.out.println("Building multipart polygon");
for(int j = 1; j < nParts; j++)
{
p = poly.getPartPoints(j);
gp.addSubPart(new GeoPolygon(gotIds ? id[i].intvalue() : i + 1, p));
}

}
map.addGeoPolygon(gp);
}

return map;
}

public PointLayer readPoints()
{
boolean gotIds = false;
Float id[] = null;
if(dbf != null)
try
{
System.out.println("I think that col " + dbf.getFieldName(idCol) + " is the system id col");
id = dbf.getFloatCol(idCol);
gotIds = true;
}
catch(IOException e)
{
System.err.println("ID's could not be read from .dbf file " + e);
}
catch(DbfFileException e)
{
System.err.println("ID's could not be read from .dbf file " + e);
}
PointLayer map = new PointLayer();
https://www.sodocs.net/doc/ea5536170.html,g.shapefile.ShapefileShape points[] = sf.getShapes();
ShapePoint p = null;
int count = sf.getRecordCount();
for(int i = 0; i < count; i++)
{
p = (ShapePoint)points[i];
GeoPoint gp = new GeoPoint(gotIds ? id[i].intvalue() : i + 1, p.getX(), p.getY());
map.addGeoPoint(gp);
}

return map;
}

}

相关主题