Hello Manisharai,

Take a look at the following classes: DataStore, FeatureSource and
FeatureIterator.

Following extract is based on GT 8, but the basic approach will be the same
for newer versions.

File file = ...
FileDataStore store = FileDataStoreFinder.getDataStore(file);
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource =
dataStore.getFeatureSource(store.getTypeNames()[0]);
SimpleFeatureIterator iterator = featureSource.getFeatures().features();
try {
   while(iterator.hasNext()){
       SimpleFeature feature = iterator.next();
       Geometry geometricObject = feature.getAttribute(0);
       Polygon polygon = (Polygon) geometricObject;
       // Do things with polygon
  }
} finally {
   iterator.close();
}

Regards,
Dieter


2014-02-12 12:57 GMT+01:00 manisharai <[email protected]>:

> I have a shape file (Sample.shp) along with two other files (Sample.shx and
> Sample.dbf), which has geometry (polygons) defined for 15 pincodes of
> Bombay.
>
> I am able to view the .shp file using the Quickstart tutorial.
>
>     File file = JFileDataStoreChooser.showOpenFile("shp", null);
>     if (file == null) {
>         return;
>     }
>
>     FileDataStore store = FileDataStoreFinder.getDataStore(file);
>     SimpleFeatureSource featureSource = store.getFeatureSource();
>
>     // Create a map content and add our shapefile to it
>     MapContent map = new MapContent();
>     map.setTitle("Quickstart");
>
>     Style style = SLD.createSimpleStyle(featureSource.getSchema());
>     Layer layer = new FeatureLayer(featureSource, style);
>     map.addLayer(layer);
>
>     // Now display the map
>     JMapFrame.showMap(map);
>
> Now I want to convert the geometry of these 15 pincodes to 15
> Geometry/Polygon objects so that I can use Geometry.contains() to find if a
> point falls in a particular Geometry/Polygon.
>
> I tried:
>
> ShapefileReader r = new ShapefileReader(new
> ShpFiles(file),true,false,geometryFactory);
> System.out.println(r.getCount(0)); >> returns 51
> System.out.println(r.hasNext()); >> returns false
>
>
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.x6.nabble.com/Extract-polygons-from-shapefile-using-Geotools-tp5103347.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
> 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
> [email protected]
> 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
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to