When you say "lies in a particular shp file", I assume you actually mean
"lies inside one of the polygons contained in a shp file"?

There may be a mode direct way (look up Filters and see if the support some
kind of "contains" feature), but what would work is to only get polygons
from the file which have a bounding box containing the point you want, and
then iterate through those to find which one(s) contain the point.

Here's some code of mine thate gets features from a Shapefile which overlap
a specified bounding box... I guess if you replaced Envelope limits with a
Point, that would work for the first part. NB... you can't access a
Shapefile's features after the Shapefile is closed, hence the forced
instantiation in the load(). If you add something like
if(!geometry.contains(specifiedPoint)) continue; the results should only
contain polygons which do contain your specified point.

Frank

  /**
   * Open a Shapefile
   *
   * @param filepath
   * @param limits
   */
  public Shapefile(String filepath, Envelope limits) {
    Log.d("Opening shapefile " + filepath);
    file = new File(filepath);
    try {
      store = FileDataStoreFinder.getDataStore(file);
      featureSource = store.getFeatureSource();
      Filter filter = CQL.toFilter("BBOX(the_geom, " + limits.getMinX() +
", " + limits.getMinY() + ", " + limits.getMaxX() + ", " + limits.getMaxY()
+ ")");
      Query query = new Query();
      query.setFilter(filter);
      query.setHints(new Hints(Hints.FEATURE_2D, Boolean.TRUE)); // force
2D data
      srcCollection = featureSource.getFeatures(filter);
    } catch (CQLException | IOException ex) {
      Util.crash(ex);
    }
  }


  /**
   * Load a Shapefile
   *
   * @param filepath
   * @param limits
   * @return
   */
  public static DefaultFeatureCollection load(String filepath, Envelope
limits) {
    Shapefile src = new Shapefile(filepath, limits);
    Log.d("Loading shapefile " + filepath);
    SimpleFeatureType schema = src.srcCollection.getSchema();
     DefaultFeatureCollection result = new DefaultFeatureCollection();
    try (SimpleFeatureIterator inputIt = src.srcCollection.features()) {
      while (inputIt.hasNext()) {
        SimpleFeature f = inputIt.next();
        // This forces instantiation of each feature
        Geometry geometry = ((Geometry) f.getDefaultGeometry()).union();
        Feature f2 = new Feature(f.getID());
        f2.setDefaultGeometry(geometry);
        Log.v(f2.getID());
        result.add(f2);
      }
      inputIt.close();
      src.close();
    }
    return result;
  }



On Tue, Feb 11, 2014 at 8:19 AM, manisharai <mraisin...@gmail.com> wrote:

> Hi
>
> I am new to GeoTools. I have a question regarding shp files.
>
> I am trying to find out if a point (lat,lon) lies in a particular shp file.
> Is there a method for the same?
>
> Can someone please provide the code if its already there?
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.x6.nabble.com/Find-if-a-point-lies-in-a-Shapefile-polygon-tp5103029.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> Androi apps run on BlackBerry 10
> Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
> Now with support for Jelly Bean, Bluetooth, Mapview and more.
> Get your Android app in front of a whole new audience.  Start now.
>
> http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
> _______________________________________________
> GeoTools-GT2-Users mailing list
> GeoTools-GT2-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to