Feature TutorialWelcomeWelcome to Geospatial for Java - this workbook is aimed at Java developers who are new to geospatial and would like to get started. You should have completed either the GeoTools NetBeans Quickstart or the GeoTools Eclipse Quickstart prior to running through this workbook. We need to be sure that you have an environment to work in with GeoTools jars and all their dependencies. For those using maven we will start off each section with the dependencies required. This workbook features a new “code first” approach. We have made every effort to make these examples both visual and code centered. We have included some background materials explaining the concepts and ideas in case you are interested. CSV2SHPWe are trying a new track for introducing features this year; rather than reading through a shapefile and ripping things apart in an artificial exercise, we are going to start by building a shapefile from scratch so you get to see every last thing that goes into creating features. The tutorial covers the following:
At the end of the tutorial you will be able to create your own custom shapefiles. Comma Separated ValueTo start with you will need a CSV file.
DependenciesPlease ensure your pom.xml includes the following: <dependencies> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>${geotools.version}</version> </dependency> </dependencies> </project> Note that the jars mentioned above will pull in a host of other dependencies (such as the hsql database driver). Main Application
Now we look at the rest of the main method in sections... Create a FeatureTypeWe create a FeatureType to describe the data that we are importing from the CSV file and writing to a shapefile. Here we use the DataUtilities convenience class: /* * We use the DataUtilities class to create a FeatureType that will describe the data in our * shapefile. * * See also the createFeatureType method below for another, more flexible approach. */ final SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:Point:srid=4326," + // <- the geometry attribute: Point type "name:String," + // <- a String attribute "number:Integer" // a number attribute ); System.out.println("TYPE:"+TYPE); Create featuresWe can now read the CSV file and create a feature for each record. Please note the following:
Note If you have used previous versions of GeoTools you might be used to creating a new FeatureCollection and using the add method to accumulate features. This usage has now been deprecated and we encourage you to treat FeatureCollections as immutable views or result sets. Create a shapefile From a FeatureCollectionThings to note as we create the shapefile:
Write the feature data to the shapefileThings to note:
This completes the main method. Prompt for the output shapefileThis method prompts the user for an appropriate shapefile to write out to. The original csv file is used to determine a good default shapefile name.
Running the ApplicationShow Your Shapefile You might like to see if you can view the new shapefile using the Quickstart.java application from the previous tutorial. When you run this application it will prompt you for:
Things to TryAnother way to build a SimpleFeatureTypeAlthough the DataUtilities class used above provided a quick and easy way to build a SimpleFeatureType, for most applications you will want to take advantage of the more flexible SimpleFeatureTypeBuilder. Here is how to use SimpleFeatureTypeBuilder to accomplish the same result:
Note the use of an upper-case constant to hold the SimpleFeatureType. Because the SimpleFeatureType class is immutable, tracking them as final variables can help you to remember that they cannot be modified once created. With this method our SimpleFeatureType contains a CoordinateReferenceSystem so there’s no need to call forceSchemaCRS to generate the ”.prj” file. Also, we are now limiting the Name field to 15 characters. Other things to try
FeatureConceptual Feature You can also draw ideas like urban growth or predicted rain fall. A feature is something that can be drawn on a map. The strict definition is that a feature is something in the real world – a feature of the landscape - Mt Everest, the Eiffel Tower, or even something that moves around like your great aunt Alice. Explaining the concept to Java developers is easy - a feature is an Object. Like a java object features can contain some information about the real world thing that they represent. This information is organized into attributes just as in Java information is slotted into fields. Occasionally you have two features that have a lot in common. You may have the LAX airport in Los Angeles and the SYD airport in Sydney. Because these two features have a couple of things in common it is nice to group them together - in Java we would create a Class called Airport. On a map we will create a Feature Type called Airport. Although it is not a capability supported by Java, early programming languages made use of a prototype system (rather than a class system) that supported lots of “one off” Objects. You will find this situation is fairly common when making maps – since how many Eiffel towers are there? You will also occasionally find the same real world thing represented a couple of different ways (the Eiffel tower can be a landmark or a tower depending on context). Here is a handy cheat sheet:
The Feature model is actually a little bit more crazy than us Java programmers are used to since it considers both attribute and operation to be “properties” of a Feature. Perhaps when Java gets closures we may be able to catch up. The really interesting thing for me is that map makers were sorting out all this stuff back in the 1400s and got every bit as geeky as programmers do now. So although we would love to teach them about object oriented programing they already have a rich set of ideas to describe the world. On the bright side, map makers are starting to use UML diagrams. FeatureClassIn GeoTools we have an interface for Feature, FeatureType and Attribute provided by the GeoAPI project. In general GeoAPI provides a very strict interface and GeoTools will provide a class. It is very common for a Feature to have only simple Attributes (String, Integer, Date and so on) rather than references to other Features, or data structures such as List<Date>. Features that meet this requirement are so common we have broken out a sub-class to represent them called SimpleFeature. At the Java level the Feature API provided by GeoTools is similar to how java.util.Map is used – it is a Java data structure used to hold information. You can look up attribute values by key; and the list of keys is provided by the FeatureType. GeometryThe other difference between an Object and a Feature is that a Feature has some form of location information (if not we would not be able to draw it on a map). The location information is going to be captured by a “Geometry” (or shape) that is stored in an attribute. We make use of the JTS Topology Suite (JTS) to represent Geometry. The JTS library provides an excellent implementation of Geometry – and gets geeky points for having a recursive acronym ! JTS is an amazing library and does all the hard graph theory to let you work with geometry in a productive fashion. Here is an example of creating a Point using the Well-Known-Text (WKT) format. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); WKTReader reader = new WKTReader( geometryFactory ); Point point = (Point) reader.read("POINT (1 1)"); You can also create a Point by hand using the GeometryFactory directly. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); Coordinate coord = new Coordinate( 1, 1 ); Point point = geometryFactory.createPoint( coord ); DataStoreWe ran into DataStore already in our Quickstart. The DataStore api is used to represent a File, Database or Service that has spatial data in it. The API has a couple of moving parts as shown below. The FeatureSource is used to read features, the subclass FeatureStore is used for read/write access. The way to tell if a File can be written to in GeoTools is to use an instanceof check. String typeNames = dataStore.getTypeNames()[0]; SimpleFeatureSource source = store.getfeatureSource( typeName ); if( source instanceof SimpleFeatureStore){ SimpleFeatureStore store = (SimpleFeatureStore) source; // write access! store.addFeatures( featureCollection ); store.removeFeatures( filter ); // filter is like SQL WHERE store.modifyFeature( attribute, value, filter ); } We decided to handle write access as a sub-class (rather than an isWritable method) in order to keep methods out of the way unless they could be used. |
|
来自: 小小文摘91gdxi > 《待分类》