📄 shapefilereader.java
字号:
//map.notifyLayerChangedListeners(LayerChangedEvent.DATA);
}
}
/**
* convinience version of get theme (shade,colname)
* provides a default satShader with the specified color
* @param colName A string containing the name of the .dbf col to shade by
* @param c A color to shade with.
*/
public Theme getTheme(String colName,Color c){
return getTheme(new satShader(c),colName);
}
/**
* Reads the feature information from the shapefile
* and produces a LineLayer for use in a Theme.
* This will only work if the feature type is
* line or polygon otherwise it will fail
* @see getShapeType
* @return LineLayer a LineLayer containing the lines from this shapefile.
*/
public Layer readLines(){
// LineLayer map = new LineLayer();
GeoLine gp;
ShapefileShape polys[] = sf.getShapes();
ShapeArc poly;
ShapePoint p[] = null;
double px[],py[];
int nPoints,nParts;
int count = sf.getRecordCount();
if(ids==null) getIds();
for(int i = 0;i<count;i++){
poly = (ShapeArc)polys[i];
nPoints = poly.getNumPoints();
nParts = poly.getNumParts();
for(int j = 0;j<nParts;j++){
p = poly.getPartPoints(j);
// px = new double[p.length];
// py = new double[p.length];
//for(int k=0;k<p.length;k++){
// px[k]=p[k].getX();
// py[k]=p[k].getY();
//}
// gp = new GeoPolygon(i+1,0,0,px,py,p.length);
gp = new GeoLine(ids[i],p);
map.addGeoLine(gp);
}
}
return map;
}
public Layer readLinesM(){
//LineLayer map = new LineLayer();
GeoLine gp;
ShapefileShape polys[] = sf.getShapes();
ShapeArcM poly;
ShapePoint p[] = null;
double px[],py[];
int nPoints,nParts;
int count = sf.getRecordCount();
for(int i = 0;i<count;i++){
poly = (ShapeArcM)polys[i];
nPoints = poly.getNumPoints();
nParts = poly.getNumParts();
for(int j = 0;j<nParts;j++){
p = poly.getPartPoints(j);
//px = new double[p.length];
//py = new double[p.length];
//for(int k=0;k<p.length;k++){
// px[k]=p[k].getX();
// py[k]=p[k].getY();
//}
gp = new GeoLine(i+1,p);
map.addGeoLine(gp);
}
}
return map;
}
/**
* Fills geodata objects with all of the data found in the shapefiles
* .dbf file and indexes it by the shapefiles idColumn.
* If during construction no ID collumn was given then it will be
* guessed based on the feature type (not always successfull)
* @return GeoData[] An array of SimpleGeoData's, one for each collum in the .dbf
*/
public GeoData[] readData(){
if(dbf != null){
GeoData data[]=new SimpleGeoData[0];
if(ids==null) getIds();
data = new SimpleGeoData[dbf.getNumFields()];
for(int j=0;j<dbf.getNumFields();j++){
data[j]=readData(j);
char t=dbf.getFieldType(j);
switch(t){
case 'C':
data[j].setDataType(GeoData.CHARACTER);
break;
case 'N':
case 'F':
if(dbf.fielddef[j].fieldnumdec==0)
data[j].setDataType(GeoData.INTEGER);
else
data[j].setDataType(GeoData.FLOATING);
break;
}
}
return data;
}
return null;
}
private int ids[];
private void getIds(){
if(ids!=null) return;
if(DEBUG)System.out.println("in getIDS "+ids+" "+idCol);
if(dbf!=null){
try{
if(idCol < dbf.getNumFields()){
if(dbf.getFieldType(idCol)=='N'){
Integer[] fids = dbf.getIntegerCol(idCol);
ids = new int[fids.length];
for(int i=0;i<fids.length;i++){
ids[i]=fids[i].intValue();
}
if(DEBUG)System.out.println("got real ids from file in getIDS "+ids);
return;
}
if(dbf.getFieldType(idCol)=='C'){ // hmm see if we can convert
if(DEBUG)System.out.println("Trying for Character based IDS");
String[] fids = dbf.getStringCol(idCol);
ids = new int[fids.length];
for(int i=0;i<fids.length;i++){
ids[i]=Integer.parseInt(fids[i].trim());
}
if(DEBUG)System.out.println("got char ids from file in getIDS "+ids);
return;
}
}
}catch(Exception e){
if(DEBUG)System.out.println(""+e);
}
if(DEBUG)System.err.println("SfR->No ID column found, using sequential ids");
ids = new int[dbf.getLastRec()];
for(int i=0;i<dbf.getLastRec();i++){
ids[i]=i+1;
}
if(DEBUG)System.out.println("got sequential ids (dbf) in getIDS "+ids);
return;
} // dbf == null
ids = new int[sf.getRecordCount()];
for(int i=0;i<sf.getRecordCount();i++){
ids[i]=i+1;
}
if(DEBUG)System.out.println("got sequential ids in getIDS "+ids);
}
/**
* Fills a geodata objects with the data found in the shapefiles
* .dbf file from the specified collumg and indexes it by the shapefiles idColumn.
* If during construction no ID collumn was given then it will be
* guessed based on the feature type (not always successfull)
* @param col An int representing the col to read the data from
* @return GeoData A SimpleGeoData
*/
/**
* Fills a geodata objects with the data found in the shapefiles
* .dbf file from the specified collumg and indexes it by the shapefiles idColumn.
* If during construction no ID collumn was given then it will be
* guessed based on the feature type (not always successfull)
* @param col An int representing the col to read the data from
* @return GeoData A SimpleGeoData
*/
public GeoData readData(int col){
SimpleGeoData data=null;
try{
if(ids==null) getIds();
data=new SimpleGeoData();
//data.setMissingValueCode(this)
data.setName(dbf.getFieldName(col).toString());
} catch(Exception e){
System.err.println("SfR->ShapeFileReader error "+
"in readData(int col,int ids[]):"+e);
}
return readData(data,col);
}
public GeoData readData(SimpleGeoData data,int col){
AddDataWhenReady dataWatch = new AddDataWhenReady(data,col);
dataWatch.start();
return data;
}
private GeoData readDataNow(SimpleGeoData data,int col){
if(dbf.getFieldType(col)=='C'){
try{
String s[] = dbf.getStringCol(col);
for(int k=0;k<s.length;k++){
data.setText(ids[k],s[k]);
data.setDataType(GeoData.CHARACTER);
}
}catch(Exception e){
// errm nothing
}
}
else{
try{
if(ids==null) getIds();
Float f[] = dbf.getFloatCol(col);
double missing = data.getMissingValueCode();
if(dbf.fielddef[col].fieldnumdec==0)
data.setDataType(GeoData.INTEGER);
else
data.setDataType(GeoData.FLOATING);
for(int k=0;k<f.length;k++){
if(null!=f[k]){
try{
data.setValue(ids[k],f[k].doubleValue());
}catch(NumberFormatException ne){
data.setValue(ids[k],missing);
}
}
else{
data.setValue(ids[k],missing);
}
}
}catch(DbfFileException d){}
catch(IOException io){}
}
return data;
}
/**
* Fills a geodata objects with the data found in the shapefiles
* .dbf file from the specified collumg and indexes it by the shapefiles idColumn.
* If during construction no ID collumn was given then it will be
* guessed based on the feature type (not always successfull)
* @param colName A String representing the col to read the data from
* @return GeoData A SimpleGeoData
*/
public GeoData readData(String colName){
SimpleGeoData data = new SimpleGeoData();
data.setName(colName);
AddDataWhenReady dataWatch = new AddDataWhenReady(data,colName);
dataWatch.run();//run directly instead of as a thread...
return data;
}
/**
* Reads the feature information from the shapefile
* and produces a PolygonLayer for use in a Theme.
* This will only work if the feature type is
* polygon otherwise it will fail.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -