【问题标题】:java.lang.NullPointerException by Geocoder with GeoTools and Shapefiles带有 GeoTools 和 Shapefile 的 Geocoder 的 java.lang.NullPointerException
【发布时间】: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


【解决方案1】:

很可能您使用了这个 gist (https://gitlab.com/-/snippets/15776) 中的示例,并且您需要更新一些导入以使其正常工作

替换

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;

这很简单,但我花了 3 个小时才找到解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2011-08-18
    • 2014-03-09
    相关资源
    最近更新 更多