📄 shapefilereader.htm
字号:
ze)<BR>{<BR>System.err.println("SFR: zip read failed " +
ze);<BR>}<BR>catch(IOException _ex)<BR>{<BR>System.err.println("SFR: zip
read not possible, looking for .shp and .dbf ");<BR>}<BR>catch(Exception
_ex)<BR>{<BR>System.err.println("SFR: General exception in zip read
");<BR>}<BR>System.out.println("Looking for files outside of zip
file");<BR>try<BR>{<BR>String ext = ".shp";<BR>String noExt =
base.getFile();<BR>if(noExt.toLowerCase().endsWith(".shp")
noExt.toLowerCase().endsWith(".zip"))<BR>noExt = name.substring(0,
name.length() - 4);<BR>URL shapeURL = new URL(base.getProtocol(),
base.getHost(), base.getPort(), noExt + ext);<BR>if(!shpZip)<BR>sf = new
Shapefile(shapeURL);<BR>ext = ".dbf";<BR>URL dbfURL = new
URL(base.getProtocol(), base.getHost(), base.getPort(), noExt +
ext);<BR>if(!dbfZip)<BR>dbf = new DbfNet(dbfURL);<BR>URL shxURL = new
URL(base.getProtocol(), base.getHost(), base.getPort(), noExt +
ext);<BR>sf.readIndex(shxURL.openStream());<BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println(e);<BR>}<BR>}<BR><BR>public
ShapefileReader(InputStream shpIn, InputStream dbfIn, int
idCol)<BR>{<BR>super();<BR>sf = null;<BR>dbf = null;<BR>this.idCol =
3;<BR>name = "none";<BR>this.idCol = idCol;<BR>try<BR>{<BR>sf = new
Shapefile(shpIn);<BR>if(dbfIn != null)<BR>dbf = new
DbfNet(dbfIn);<BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println(e);<BR>}<BR>}<BR><BR>public
ShapefileReader(InputStream shpIn, InputStream
dbfIn)<BR>{<BR>super();<BR>sf = null;<BR>dbf = null;<BR>idCol = 3;<BR>name
= "none";<BR>try<BR>{<BR>sf = new Shapefile(shpIn);<BR>if(dbfIn !=
null)<BR>dbf = new DbfNet(dbfIn);<BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println(e);<BR>}<BR>guessIdCol();<BR>}<BR><BR>public
ShapefileReader(URL base)<BR>{<BR>this(base,
0);<BR>guessIdCol();<BR>}<BR><BR>private void
guessIdCol()<BR>{<BR>switch(sf.getShapeType())<BR>{<BR>case 5: //
'\005'<BR>idCol = 2;<BR>break;<BR><BR>case 23: // '\027'<BR>idCol =
5;<BR>break;<BR><BR>case 3: // '\003'<BR>idCol = 5;<BR>break;<BR><BR>case
1: // '\001'<BR>idCol = 0;<BR>break;<BR><BR>}<BR>if(idCol >
dbf.getNumFields() - 1)<BR>idCol = 0;<BR>System.out.println("ID col
guessed at " + idCol + " " + dbf.getNumFields());<BR>}<BR><BR>public void
setIdCol(int col)<BR>{<BR>idCol = col;<BR>}<BR><BR>public void
setIdCol(String name)<BR>{<BR>int col = dbf.getFieldNumber(name);<BR>idCol
= col >= 0 ? col : idCol;<BR>}<BR><BR>public int
getShapeType()<BR>{<BR>return sf.getShapeType();<BR>}<BR><BR>public Theme
getTheme()<BR>{<BR>Layer l = null;<BR>switch(getShapeType())<BR>{<BR>case
5: // '\005'<BR>l = readPolygons();<BR>break;<BR><BR>case 3: //
'\003'<BR>l = readLines();<BR>break;<BR><BR>case 1: // '\001'<BR>l =
readPoints();<BR>break;<BR><BR>case 23: // '\027'<BR>l =
readLinesM();<BR>break;<BR><BR>default:<BR>return null;<BR><BR>}<BR>Theme
t = new Theme(l);<BR>t.setName(name);<BR>return t;<BR>}<BR><BR>public
Theme getTheme(Shader shade, String colName)<BR>{<BR>Theme t =
getTheme();<BR>if(colName != null)<BR>{<BR>int col =
dbf.getFieldNumber(colName);<BR>if(col >= 0)<BR>{<BR>GeoData d =
readData(col);<BR>shade.setRange(d);<BR>t.setShader(shade);<BR>t.setGeoData(d);<BR>}<BR>else<BR>{<BR>System.err.println("Column
" + colName + " not found in .dbf file");<BR>}<BR>}<BR>return
t;<BR>}<BR><BR>public Theme getTheme(String colName, Color
c)<BR>{<BR>return getTheme(new satShader(c), colName);<BR>}<BR><BR>public
LineLayer readLines()<BR>{<BR>LineLayer map = new
LineLayer();<BR>uk.ac.leeds.ccg.shapefile.ShapefileShape polys[] =
sf.getShapes();<BR>ShapePoint p[] = null;<BR>int count =
sf.getRecordCount();<BR>for(int i = 0; i < count; i++)<BR>{<BR>ShapeArc
poly = (ShapeArc)polys[i];<BR>int nPoints = poly.getNumPoints();<BR>int
nParts = poly.getNumParts();<BR>for(int j = 0; j < nParts;
j++)<BR>{<BR>p = poly.getPartPoints(j);<BR>GeoPolygon gp = new
GeoPolygon(i + 1,
p);<BR>map.addGeoPolygon(gp);<BR>}<BR><BR>}<BR><BR>return
map;<BR>}<BR><BR>public LineLayer readLinesM()<BR>{<BR>LineLayer map = new
LineLayer();<BR>uk.ac.leeds.ccg.shapefile.ShapefileShape polys[] =
sf.getShapes();<BR>ShapePoint p[] = null;<BR>int count =
sf.getRecordCount();<BR>for(int i = 0; i < count;
i++)<BR>{<BR>ShapeArcM poly = (ShapeArcM)polys[i];<BR>int nPoints =
poly.getNumPoints();<BR>int nParts = poly.getNumParts();<BR>for(int j = 0;
j < nParts; j++)<BR>{<BR>p = poly.getPartPoints(j);<BR>double px[] =
new double[p.length];<BR>double py[] = new double[p.length];<BR>for(int k
= 0; k < p.length; k++)<BR>{<BR>px[k] = p[k].getX();<BR>py[k] =
p[k].getY();<BR>}<BR><BR>GeoPolygon gp = new GeoPolygon(i + 1, 0.0D, 0.0D,
px, py, p.length);<BR>map.addGeoPolygon(gp);<BR>}<BR><BR>}<BR><BR>return
map;<BR>}<BR><BR>public GeoData[] readData()<BR>{<BR>GeoData data[] = new
SimpleGeoData[0];<BR>if(dbf != null)<BR>try<BR>{<BR>int ids[] =
getIds();<BR>data = new SimpleGeoData[dbf.getNumFields()];<BR>for(int j =
0; j < dbf.getNumFields(); j++)<BR>data[j] = readData(j,
ids);<BR><BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println("ShapeFileReader error :" +
e);<BR>}<BR>return data;<BR>}<BR><BR>private int[] getIds()<BR>throws
IOException, DbfFileException<BR>{<BR>int
ids[];<BR>if(dbf.getFieldType(idCol) == 'N')<BR>{<BR>Float fids[] =
dbf.getFloatCol(idCol);<BR>ids = new int[fids.length];<BR>for(int i = 0; i
< fids.length; i++)<BR>ids[i] = fids[i].intValue();<BR><BR>return
ids;<BR>}<BR>System.err.println("No ID column found, using sequential
ids");<BR>ids = new int[dbf.getLastRec()];<BR>for(int i = 0; i <
dbf.getLastRec(); i++)<BR>ids[i] = i + 1;<BR><BR>return
ids;<BR>}<BR><BR>public GeoData readData(int
col)<BR>{<BR>try<BR>{<BR>return readData(col,
getIds());<BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println("ShapeFileReader error :" +
e);<BR>}<BR>return null;<BR>}<BR><BR>public GeoData readData(int col, int
ids[])<BR>{<BR>SimpleGeoData data = null;<BR>try<BR>{<BR>data = new
SimpleGeoData();<BR>data.setName(dbf.getFieldName(col).toString());<BR>if(dbf.getFieldType(col)
!= 'N')<BR>{<BR>String s[] = dbf.getStringCol(col);<BR>for(int k = 0; k
< s.length; k++)<BR>data.setText(ids[k],
s[k]);<BR><BR>}<BR>else<BR>{<BR>Float f[] =
dbf.getFloatCol(col);<BR>double missing =
data.getMissingValueCode();<BR>for(int k = 0; k < f.length;
k++)<BR>if(f[k] != null)<BR>data.setValue(ids[k],
f[k].doubleValue());<BR>else<BR>data.setValue(ids[k],
missing);<BR><BR>}<BR>}<BR>catch(Exception
e)<BR>{<BR>System.err.println("ShapeFileReader error :" +
e);<BR>}<BR>return data;<BR>}<BR><BR>public GeoData readData(String
colName)<BR>{<BR>if(colName != null)<BR>{<BR>int col =
dbf.getFieldNumber(colName);<BR>if(col >= 0)<BR>return
readData(col);<BR>System.err.println("Column " + colName + " could not be
found");<BR>}<BR>return null;<BR>}<BR><BR>public PolygonLayer
readPolygons()<BR>{<BR>boolean gotIds = false;<BR>Float id[] =
null;<BR>if(dbf != null)<BR>try<BR>{<BR>System.out.println("I think that
col " + dbf.getFieldName(idCol) + " is the system id col");<BR>id =
dbf.getFloatCol(idCol);<BR>gotIds = true;<BR>}<BR>catch(IOException
e)<BR>{<BR>System.err.println("ID's could not be read from .dbf file " +
e);<BR>}<BR>catch(DbfFileException e)<BR>{<BR>System.err.println("ID's
could not be read from .dbf file " +
e);<BR>}<BR>if(!gotIds)<BR>System.out.println("Using sequential
ids");<BR>PolygonLayer map = new
PolygonLayer();<BR>uk.ac.leeds.ccg.shapefile.ShapefileShape polys[] =
sf.getShapes();<BR>ShapePoint p[] = null;<BR>int count =
sf.getRecordCount();<BR>for(int i = 0; i < count;
i++)<BR>{<BR>ShapePolygon poly = (ShapePolygon)polys[i];<BR>int nPoints =
poly.getNumPoints();<BR>int nParts = poly.getNumParts();<BR>p =
poly.getPartPoints(0);<BR>GeoPolygon gp = new GeoPolygon(gotIds ?
id[i].intValue() : i + 1, p);<BR>if(nParts >
1)<BR>{<BR>System.out.println("Building multipart polygon");<BR>for(int j
= 1; j < nParts; j++)<BR>{<BR>p =
poly.getPartPoints(j);<BR>gp.addSubPart(new GeoPolygon(gotIds ?
id[i].intValue() : i + 1,
p));<BR>}<BR><BR>}<BR>map.addGeoPolygon(gp);<BR>}<BR><BR>return
map;<BR>}<BR><BR>public PointLayer readPoints()<BR>{<BR>boolean gotIds =
false;<BR>Float id[] = null;<BR>if(dbf !=
null)<BR>try<BR>{<BR>System.out.println("I think that col " +
dbf.getFieldName(idCol) + " is the system id col");<BR>id =
dbf.getFloatCol(idCol);<BR>gotIds = true;<BR>}<BR>catch(IOException
e)<BR>{<BR>System.err.println("ID's could not be read from .dbf file " +
e);<BR>}<BR>catch(DbfFileException e)<BR>{<BR>System.err.println("ID's
could not be read from .dbf file " + e);<BR>}<BR>PointLayer map = new
PointLayer();<BR>uk.ac.leeds.ccg.shapefile.ShapefileShape points[] =
sf.getShapes();<BR>ShapePoint p = null;<BR>int count =
sf.getRecordCount();<BR>for(int i = 0; i < count; i++)<BR>{<BR>p =
(ShapePoint)points[i];<BR>GeoPoint gp = new GeoPoint(gotIds ?
id[i].intValue() : i + 1, p.getX(),
p.getY());<BR>map.addGeoPoint(gp);<BR>}<BR><BR>return
map;<BR>}<BR><BR>}<BR><BR><BR><BR>GML--Geography Markup
Language<BR><BR>网络地理信息系统的必然选择,<BR><BR>难道你没听说过?呵呵,没关系!<BR><BR>一切从头开始!</P></TD></TR></TBODY></TABLE></DIV>
<DIV align=center>
<CENTER>
<TABLE border=1 borderColorDark=#79bcff borderColorLight=#79bcff cellPadding=0
cellSpacing=0 height=20 width=629>
<TBODY>
<TR>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom width=104> </TD>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom width=104> </TD>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom width=104> </TD>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom width=105> </TD>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom width=105> </TD>
<TD align=middle bgColor=#dfefff height=20 vAlign=bottom
width=105> </TD></TR></TBODY></TABLE></CENTER></DIV>
<DIV align=center>
<CENTER>
<TABLE border=0 cellPadding=0 cellSpacing=0 height=20 width=629>
<TBODY>
<TR>
<TD height=20 width=625></TD></TR></TBODY></TABLE></CENTER></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -