From: Wouter S. <wou...@gm...> - 2008-08-21 15:56:40
|
Hi Hari, I don't have any example with the 2.5 version, but this is how i do it with the 2.4 version: Map connect = new HashMap(); if (file!=null){ try { connect.put( "url", file.toURI().toURL()); ShapefileDataStore dataStore = (ShapefileDataStore) DataStoreFinder.getDataStore( connect ); String[] typeNames = dataStore.getTypeNames (); String typeName = typeNames[0]; dataStore.forceSchemaCRS(featureType.getDefaultGeometry().getCoordinateSystem()); logger.debug("Shapefile typename: "+typeName); FeatureSource featureSource = dataStore.getFeatureSource( typeName ); FeatureCollection collection = featureSource.getFeatures(); FeatureIterator iterator = collection.features(); try { while( iterator.hasNext() ){ Feature feature = iterator.next(); Geometry geometry = (Geometry) feature.getAttribute("the_geom"); if (geometry.getNumGeometries()>1){ for (int i = 0; i < geometry.getNumGeometries(); i++){ lr.addObject(createFeature(geometry.getGeometryN(i))); } } else if (geometry.getGeometryType().startsWith("Multi")){ lr.addObject(createFeature(geometry.getGeometryN(0))); } else lr.addObject(createFeature(geometry)); } } finally { iterator.close(); dataStore.dispose(); } } catch (MalformedURLException e) { e.printStackTrace(); throw new DaoException(e); } catch (IOException e) { e.printStackTrace(); throw new DaoException(e); } } This example extracts all the geometries out of a shapefile, and puts them in a custom object (the lr-object) hope this helps? grtz, w On Thu, Aug 21, 2008 at 4:50 PM, Nair, Hari <Har...@jh...> wrote: > I just started with geotools yesterday, and I'm having trouble with the > "Reading a shapefile" example in the user's guide. > > Here are a few problems: > > 1) The repository locations don't seem to be correct. Ibiblio and > repo1.maven.org don't have org.geotools directories. > > 2) The artifactId's in the pom.xml file are given as gt2-*, but the 2.5 > release only has gt-*, right? There are no 2.5-M3 directories in the > repositories for gt2-*. > > 3) The example itself has errors in eclipse. Specifically, > Geometry geometry = feature.getDefaultGeometry(); > is not legal. There are warnings for the usage of Map and the Feature > objects. > > Are there significant updates needed to this example to get it to work with > 2.5-M3? > > Ultimately, I want to be able to read shapefiles and reproject them from > lon/lat to pixel coordinates to overlay on a polar stereographic map. I > already have the reprojection code written in Java so I wanted java code to > read shapefiles. > > Thanks, > Hari > > > -- > Hari Nair > Mail Stop MP2-S128 > Applied Physics Laboratory > 11100 Johns Hopkins Road > Laurel, MD 20723 > (443) 778-4017 > > > > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > Geotools-gt2-users mailing list > Geo...@li... > https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users > |