java.lang.NullPointerException 由 Geocoder 使用 GeoTools 和 Shapefiles 引发

huangapple 未分类评论68阅读模式
英文:

java.lang.NullPointerException by Geocoder with GeoTools and Shapefiles

问题

我是新手,尝试使用Geotools中的Reverse Geocode脚本。但我总是收到java.lang.NullPointerException错误。有人能帮我吗?

> 在线程 "main" 中的异常 java.lang.NullPointerException
> 位于 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);
    }
}
英文:

I am new to java and try to use a Reverse Geocode Script from Geotools. But I always get a java.lang.NullPointerException. Can someone help me?

> Exception in thread "main" java.lang.NullPointerException at
> org.locationtech.jts.geom.Envelope.intersects(Envelope.java:528) at
> org.locationtech.jts.index.strtree.STRtree$3.intersects(STRtree.java:108)
> at
> org.locationtech.jts.index.strtree.AbstractSTRtree.query(AbstractSTRtree.java:256)
> at org.locationtech.jts.index.strtree.STRtree.query(STRtree.java:205)
> at
> org.geotools.data.collection.SpatialIndexFeatureCollection.subCollection(SpatialIndexFeatureCollection.java:152)
> at com.company.SimpleGeoCoder.lookup(SimpleGeoCoder.java:53) at
> 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(&quot;NAME&quot;));
                }
            } finally {
                itr.close();
            }
                            }
                }



    public class SimpleGeoCoder {
    
            SpatialIndexFeatureCollection countries;
            final static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
            final static public String countryFile = &quot;countries.shp&quot;;
    
            public SimpleGeoCoder() throws IOException {
                URL countryURL = URLs.fileToUrl(new File(this.countryFile));
                HashMap&lt;String, Object&gt; params = new HashMap&lt;&gt;();
                params.put(&quot;url&quot;, countryURL);
                DataStore ds = DataStoreFinder.getDataStore(params);
                if (ds == null) {
                    throw new IOException(&quot;couldn&#39;t open &quot; + params.get(&quot;url&quot;));
                }
                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(&quot;the_geom&quot;), ff.literal(p));
              return this.countries.subCollection(f);
            }
        }

答案1

得分: 0

可能你使用了这个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个小时才找到如何修复它。

英文:

Most probably you used the example from this gist (https://gitlab.com/-/snippets/15776) and yon need to update some imports to make it work

Replace

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

with

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

It was so simple, but it took me 3 hours to find out how to fix it.

huangapple
  • 本文由 发表于 2020年7月29日 00:21:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63138591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定