CityGmlIterator.java 1.92 KB
Newer Older
duminil's avatar
duminil committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package eu.simstadt.lowlevelgmlparser;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.logging.Logger;
import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;


public class CityGmlIterator implements Iterable<String>
{

	private static final Logger LOGGER = Logger.getLogger(CityGmlIterator.class.getName());

	private AutoPilot buildingsFinder;
	private VTDNav navigator;
	private long offsetAndLength;
	private int buildingsCount = 0;
	private int buildingOffset = 0;
	private int buildingLength = 0;

	public CityGmlIterator(Path citygmlPath)
			throws XPathParseException, NavException, NumberFormatException, XPathEvalException, IOException {
		VTDGen parser = new VTDGen();
		parser.parseFile(citygmlPath.toString(), false);
		this.navigator = parser.getNav();
		this.buildingsFinder = new AutoPilot(navigator);
		buildingsFinder.selectXPath("//cityObjectMember");
	}

	@Override
	public Iterator<String> iterator() {
		Iterator<String> it = new Iterator<String>() {

			@Override
			public boolean hasNext() {
				try {
					return buildingsFinder.evalXPath() != -1;
				} catch (XPathEvalException | NavException ex) {}
				return false;
			}

			@Override
			public String next() {
				try {
					buildingsCount += 1;
					if (buildingsCount % 1000 == 0) {
						LOGGER.info("1000 buildings parsed");
					}
					offsetAndLength = navigator.getElementFragment();
					buildingOffset = (int) offsetAndLength;
					buildingLength = (int) (offsetAndLength >> 32);
duminil's avatar
duminil committed
58
59
					BuildingXmlNode b = new BuildingXmlNode(navigator, buildingOffset, buildingLength);
					return b.toString();
duminil's avatar
duminil committed
60
61
62
63
64
65
66
67
68
69
70
71
72
				} catch (NavException ex) {}
				return null;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
		return it;
	}

}