?? miffile.java
字號:
coords[p] = readMIFCoordinate();
}
numpoints = 0;
lineStrings[i] = geomFactory.createLineString(coords);
}
if ((numsections == 1) && !toGeometryCollection) {
return (Geometry) lineStrings[0];
}
return (Geometry) geomFactory.createMultiLineString(lineStrings);
} catch (Exception e) {
throw new IOException(
"Exception reading PLine data from MIF file : "
+ e.toString());
}
}
/**
* Reads Region (Polygon) information from the MIF stream
*
* @return The (MULTI)POLYGON object
*
* @throws IOException Error retrieving geometry from input MIF stream
*/
private Geometry readRegionObject() throws IOException {
try {
int numpolygons = Integer.parseInt(mif.getToken(' ', true));
Vector polygons = new Vector();
LinearRing tmpRing = null;
Polygon shell = null;
LinearRing shellRing = null;
Vector holes = null;
boolean savePolygon;
// Read all linearrings;
for (int i = 0; i < numpolygons; i++) {
// Read coordinates & create ring
int numpoints = Integer.parseInt(mif.getToken(' ', true));
Coordinate[] coords = new Coordinate[numpoints + 1];
for (int p = 0; p < numpoints; p++) {
coords[p] = readMIFCoordinate();
}
coords[coords.length - 1] = coords[0];
tmpRing = geomFactory.createLinearRing(coords);
/*
* In MIF format a polygon is described as a list of rings, with no info wether
* a ring is a hole or a shell, so we have to determine it by checking if
* a ring in contained in the previously defined shell
*/
if ((shell != null) && shell.contains(tmpRing)) {
holes.add(tmpRing);
tmpRing = null; // mark as done
savePolygon = (i == (numpolygons - 1));
} else {
// New polygon, must save previous if it's not the first ring
savePolygon = (i > 0);
}
if (savePolygon) {
LinearRing[] h = null;
if (holes.size() > 0) {
h = new LinearRing[holes.size()];
for (int hole = 0; hole < holes.size(); hole++) {
h[hole] = (LinearRing) holes.get(hole);
}
}
polygons.add(geomFactory.createPolygon(shellRing, h));
shellRing = null;
}
// Build the polygon needed for testing holes
if (tmpRing != null) {
shellRing = tmpRing;
shell = geomFactory.createPolygon(shellRing, null);
holes = new Vector();
}
}
if (shellRing != null) {
polygons.add(geomFactory.createPolygon(shellRing, null));
}
try {
if ((polygons.size() == 1) && !toGeometryCollection) {
return (Polygon) polygons.get(0);
}
Polygon[] polys = new Polygon[polygons.size()];
for (int i = 0; i < polygons.size(); i++) {
polys[i] = (Polygon) polygons.get(i);
}
return geomFactory.createMultiPolygon(polys);
} catch (TopologyException topexp) {
throw new TopologyException(
"TopologyException reading Region polygon : "
+ topexp.toString());
}
} catch (Exception e) {
throw new IOException(
"Exception reading Region data from MIF file : "
+ e.toString());
}
}
/**
* Reads a couple of coordinates (x,y) from input stream, applying the
* transform factor if required.
*
* @return A Coordinate object, or null if error encountered
*
* @throws IOException if couldn't build a valid Coordinate object
*/
private Coordinate readMIFCoordinate() throws IOException {
String x;
String y;
try {
x = mif.getToken(' ', true);
y = mif.getToken();
if (x.equals("") || y.equals("")) {
throw new IOException("End of file.");
}
Coordinate result = new Coordinate(Double.parseDouble(x),
Double.parseDouble(y));
if (useTransform) {
result.x = (result.x * multX) + sumX;
result.y = (result.y * multY) + sumY;
}
return result;
} catch (Exception e) {
throw new IOException("Error getting coordinates: "
+ e.toString());
}
}
/**
* Reads Point information from the MIF stream
*
* @return The next POINT object read
*
* @throws IOException Error retrieving geometry from input MIF stream
*/
private Geometry readPointObject() throws IOException {
return geomFactory.createPoint(readMIFCoordinate());
}
/**
* Reads Line information from the MIF stream
*
* @return a LINESTRING object
*
* @throws IOException Error retrieving geometry from input MIF stream
*/
private Geometry readLineObject() throws IOException {
Coordinate[] cPoints = new Coordinate[2];
cPoints[0] = readMIFCoordinate();
cPoints[1] = readMIFCoordinate();
LineString[] result = { geomFactory.createLineString(cPoints) };
if (toGeometryCollection) {
return geomFactory.createMultiLineString(result);
}
return result[0];
}
private Geometry readTextObject() throws IOException {
try {
mifText = mif.getToken(' ', true, true);
} catch (ParseException e) {
throw new IOException(e.getMessage());
}
Coordinate c1 = readMIFCoordinate();
Coordinate c2 = readMIFCoordinate();
Coordinate p = new Coordinate((c1.x + c2.x) / 2, (c1.y + c2.y) / 2);
return geomFactory.createPoint(p);
}
}
/**
* <p>
* MIF FeatureWriter
* </p>
*/
private class Writer implements FeatureWriter<SimpleFeatureType, SimpleFeature> {
private PrintStream outMif = null;
private PrintStream outMid = null;
private FeatureReader<SimpleFeatureType, SimpleFeature> innerReader = null;
private MIFValueSetter[] fieldValueSetters;
private SimpleFeature editFeature = null;
private SimpleFeature originalFeature = null;
private Writer(PrintStream mif, PrintStream mid, boolean append)
throws IOException {
innerReader = getFeatureReader();
try {
fieldValueSetters = getValueSetters();
} catch (SchemaException e) {
throw new IOException(e.getMessage());
}
outMif = mif;
outMid = mid;
try {
if (!append) {
outMif.println(exportHeader());
}
} catch (Exception e) {
outMif = null;
outMid = null;
throw new IOException(e.getMessage());
}
}
public SimpleFeatureType getFeatureType() {
return featureType;
}
public SimpleFeature next() throws IOException {
try {
if (originalFeature != null) {
writeFeature(originalFeature); // keep the original
}
if (innerReader.hasNext()) {
originalFeature = innerReader.next(); // ;
editFeature = SimpleFeatureBuilder.copy(originalFeature);
} else {
originalFeature = null;
editFeature = SimpleFeatureBuilder.build(featureType, featureDefaults, null);
}
return editFeature;
} catch (Exception e) {
throw new IOException(e.toString());
}
}
public void remove() throws IOException {
if (editFeature == null) {
throw new IOException("Current feature is null");
}
editFeature = null;
originalFeature = null;
}
public void write() throws IOException {
if (editFeature == null) {
throw new IOException("Current feature is null");
}
try {
writeFeature(editFeature);
} catch (Exception e) {
editFeature = null;
throw new IOException("Can't write feature: " + e.toString());
}
editFeature = null;
originalFeature = null;
}
public boolean hasNext() throws IOException {
return innerReader.hasNext();
}
public void close() throws IOException {
while (hasNext())
next();
try {
if (originalFeature != null) {
writeFeature(originalFeature); // keep the original
}
} catch (Exception e) {
}
innerReader.close();
innerReader = null;
try {
if (outMif != null) {
outMif.close();
}
if (outMid != null) {
outMid.close();
}
copyFileAndDelete(mifFileOut, mifFile, true);
copyFileAndDelete(midFileOut, midFile, true);
} catch (IOException e) {
} finally {
outMid = null;
outMif = null;
}
}
protected void finalize() throws Throwable {
close();
super.finalize();
}
/**
* Writes the given Feature to file
*
* @param f The feature to write
*
* @throws IOException if cannot access file for reading
* @throws SchemaException if given Feature is not compatible with
* MIFFile FeatureType. TODO: private
*/
public void writeFeature(SimpleFeature f) throws IOException, SchemaException {
if ((outMif == null) || (outMid == null)) {
throw new IOException(
"Output stream has not been opened for writing.");
}
Geometry theGeom = (geomFieldIndex >= 0)
? (Geometry) f.getAttribute(geomFieldIndex) : null;
String outGeom = exportGeometry(theGeom);
if (outGeom.equals("")) {
throw new SchemaException("Unsupported geometry type: "
+ theGeom.getClass().getName());
}
outMif.println(outGeom);
int col;
String outBuf = "";
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -