【发布时间】:2020-07-28 16:21:44
【问题描述】:
我是 java 新手,尝试使用 Geotools 的反向地理编码脚本。但我总是得到一个 java.lang.NullPointerException。有人可以帮我吗?
线程“main”中的异常 java.lang.NullPointerException at org.locationtech.jts.geom.Envelope.intersects(Envelope.java:528) 在 org.locationtech.jts.index.strtree.STRtree$3.intersects(STRtree.java:108) 在 org.locationtech.jts.index.strtree.AbstractSTRtree.query(AbstractSTRtree.java:256) 在 org.locationtech.jts.index.strtree.STRtree.query(STRtree.java:205) 在 org.geotools.data.collection.SpatialIndexFeatureCollection.subCollection(SpatialIndexFeatureCollection.java:152) 在 com.company.SimpleGeoCoder.lookup(SimpleGeoCoder.java:53) 在 com.company.Main.main(Main.java:60)
public class Main {
public static void main(String[] args) throws IOException {
GeometryFactory gf = new GeometryFactory();
SimpleGeoCoder geocoder = new SimpleGeoCoder();
Point london = gf.createPoint(new Coordinate(0.0, 51.0));
SimpleFeatureCollection features = geocoder.lookup(london);
SimpleFeatureIterator itr = features.features();
try {
while (itr.hasNext()) {
SimpleFeature f = itr.next();
System.out.println(f.getAttribute("NAME"));
}
} finally {
itr.close();
}
}
}
public class SimpleGeoCoder {
SpatialIndexFeatureCollection countries;
final static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
final static public String countryFile = "countries.shp";
public SimpleGeoCoder() throws IOException {
URL countryURL = URLs.fileToUrl(new File(this.countryFile));
HashMap<String, Object> params = new HashMap<>();
params.put("url", countryURL);
DataStore ds = DataStoreFinder.getDataStore(params);
if (ds == null) {
throw new IOException("couldn't open " + params.get("url"));
}
Name name = ds.getNames().get(0);
this.countries = new SpatialIndexFeatureCollection(ds.getFeatureSource(name).getFeatures());
}
public SimpleFeatureCollection lookup(Point p) {
Filter f = ff.contains(ff.property("the_geom"), ff.literal(p));
return this.countries.subCollection(f);
}
}
【问题讨论】:
-
我刚刚尝试了您的代码,当我使用 Natural Earth 10M 国家/地区大纲时(当我将 NAME 更改为名称时)它适用于我 - 您从哪里获得国家/地区文件?它包含什么?
-
这很有趣。我正在使用来自naturalearthdata.com 的 10m_admin_0_countries.shp。会不会和Java版本或者Geotool版本有关?
-
很难说你什么时候没有在你的问题中指定:-)
-
当然 xD。我正在使用 IntelliJ IDEA、java 版本 13.0.1、Geotools 版本 24 快照。我尝试使用 System.out.println(); 进行测试检查丢失的内容,但似乎总是将值传输到最后一行代码(返回 this.countries.subCollection(f);)。
-
GeoTools 不支持 8 和 11 以外的 Java 版本。
标签: java nullpointerexception reverse-geocoding geotools