Geotools application-Data

Source: Internet
Author: User

1. geotoolsThe open source Java GIS Toolkit

Http://geotools.org/geotools Official Website
Http://docs.geotools.org/latest/javadocs/ geotools API online documentation
Http://docs.codehaus.org/display/GEOTDOC/Home geotools User Guide
Maven repository address for http://repo.opengeo.org geotools
Http://download.osgeo.org/webdav/geotools/ Maven repository address

Pom. xml configuration

<repositories>    <repository>        <id>osgeo</id>        <name>Open Source Geospatial Foundation Repository</name>        <url>http://download.osgeo.org/webdav/geotools/</url>    </repository>    <repository>        <snapshots>            <enabled>true</enabled>        </snapshots>        <id>opengeo</id>        <name>OpenGeo Maven Repository</name>        <url>http://repo.opengeo.org</url>    </repository></repositories>

Eg: gets the dependency of the gt-main.jar

<dependency><groupId>org.geotools</groupId><artifactId>gt-main</artifactId><version>8.4</version></dependency>

Ii. OpenGIS Software Architecture

Org. geotools. Data
The package is responsible for reading and writing geographical data (for example, shavefilereader is used to read shpfile data), org. geotools. Geometry
The package provides interfaces for calling JTs to encapsulate geographical data into a geometry defined in JTs ),
The Org. geotools. Feature package encapsulates the Space geometric element object (feature), which corresponds to an object in the map,
Including: space data (geometry), attribute data (aitriry), reference coordinate system (refereneedsystem ),
Attributes such as the smallest outsourcing rectangle (envelope) are the core data model of Gls operations.

Example of geotools reading SHP data format:

/*** Read the shap Format File ** @ Param path */Public void readshp (string path) {shapefiledatastore shpdatastore = NULL; try {shpdatastore = new shapefiledatastore (new file (PATH ). touri (). tourl (); shpdatastore. setstringcharset (charset. forname ("GBK"); // file name string typename = shpdatastore. gettypenames () [0]; featuresource <simplefeaturetype, simplefeature> featuresource = NULL; featuresource = (featuresource <simplefeaturetype, simplefeature>) shpdatastore. getfeaturesource (typename); featurecollection <simplefeaturetype, simplefeature> result = featuresource. getfeatures (); simplefeaturetype schema = result. getschema (); // schemalist <attributedescriptor> Columns = schema. getattributedescriptors (); featureiterator <simplefeature> itertor = result. features ();/** or use featurereader reader = * datautilities. reader (result); While (reader. hasnext () {* simplefeature feature = (simplefeature) reader. next ();} */while (itertor. hasnext () {simplefeature feature = itertor. next (); For (attributedescriptor attributedes: columns) {string attributename = attributedes. getname (). tostring (); // attributeif (attributename. equals ("the_geom") continue; feature. getattribute (attributename); // attributevalue} geometry G = (geometry) feature. getdefageometry (); // geometry} itertor. close ();} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}

/*** Read the file in DBF format. Only the attribute value is stored, and the value of the bucket is not ** @ Param path */Public void readdbf (string path) {dbasefilereader reader = NULL; try {reader = new dbasefilereader (New shpfiles (PATH), false, charset. forname ("GBK"); dbasefileheader header = reader. getheader (); int numfields = header. getnumfields (); For (INT I = 0; I <numfields; I ++) {header. getfieldname (I); header. getfieldtype (I); // 'C', 'n' header. getfieldlength (I );}// Iterator read record while (reader. hasnext () {try {object [] entry = reader. readentry (); For (INT I = 0; I <numfields; I ++) {String title = header. getfieldname (I); object value = entry [I]; string name = title. tostring (); // columnstring info = value. tostring (); // value} catch (exception e) {e. printstacktrace () ;}} catch (exception ex) {ex. printstacktrace ();} finally {If (reader! = NULL) {// close try {reader. Close () ;}catch (exception e ){}}}}

Output a shp File

/*** Create a SHP file ** @ Param outpath */Public void createshp (string outpath) {try {// define the attribute final simplefeaturetype = datautilities. createtype ("location", "Location: Point," + "Name: String," + "info: String," + "Owner: string"); featurecollection <simplefeaturetype, simplefeature> collection = featurecollections. newcollection (); geometryfactory = new geometryfactory (); simplefeaturebuilder featurebuilder = new simplefeaturebuilder (type); double latitude = double. parsedouble ("116.123456789"); double longings = double. parsedouble ("39.120001"); string name = "express 110"; string info = "White shuttle bus, student pass valid"; string owner = "001 "; // create the coordinate point = geometryfactory. createpoint (new coordinate (longpolling, latitude); // create the attribute value object [] OBJ = {point, name, info, owner}; // construct a featuresimplefeature feature = featurebuilder. buildfeature (null, OBJ); // Add to collection. add (feature); // shap file output path file newfile = new file (outpath); Map <string, serializable> Params = new hashmap <string, serializable> (); params. put ("url", (serializable) newfile. touri (). tourl (); Params. put ("create spatial index", (serializable) Boolean. true); shapefiledatastorefactory datastorefactory = new shapefiledatastorefactory (); shapefiledatastore newdatastore = (shapefiledatastore) datastorefactory. createnewdatastore (Params); newdatastore. createschema (type); newdatastore. setstringcharset (charset. forname ("GBK"); newdatastore. forceschemacrs (defageogeographiccrs. WGS84); string typename = newdatastore. gettypenames () [0]; shapefilefeaturelocking featuresource = (shapefilefeaturelocking) newdatastore. getfeaturesource (typename); // create a transaction = new defaulttransaction ("CREATE"); featuresource. settransaction (transaction); try {featuresource. addfeatures (Collection); // submit the transaction. commit ();} catch (exception problem) {problem. printstacktrace (); transaction. rollback ();} finally {transaction. close () ;}} catch (exception e) {e. printstacktrace ();}}

Org. geotools. Data. datautilities
A facade classes which can help simplify common data wrangling chores simplify complicated general data
(1) define attributes
Featuretype type = datautilities. createtype ("location ",
"Location: Point," + "Name: String," + "info: String," + "Owner: string ");

(2) datautilities. Schema
You can use this method to quickly get a representation of a featuretype to return the schema of featuretype
// Return Schema
Datautilities. SPEC (featuretype ))

(3) datautilities. Collection feature array to feature collection
Datautilities has helper methods to turn almost anything into a featurecollection
Feature [] array;
....
Return datautilties. Collection (array );

(4) datautilities. Reader format
Convert a perfectly good collection to featurereader format.
Featurecollection collection;
Featurereader reader = datautilities. Reader (Collection );

Appendix: Introduction to SHP files
Shapefile file extensions
. SHP-the main file that stores the Feature Geometry. required.
. Shx-the index file that stores the index of the Feature Geometry. required.
. DBF-The DBASE table that stores the attribute information of features. required. There is a one-to-one relationship between geometry and attributes, which is based on record number.
. Prj-the file that stores the coordinate system information. Used by ArcGIS.

Fieldtype in the dbf File
Data allowed by the code data type
B: binary characters.
C character type.
The D-type is used to differentiate the numbers of years, months, and days and a character. The internal storage adopts the yyyymmdd format.
G (generalor OLE) characters.
N numeric type (numeric)-. 0 1 2 3 4 5 6 7 8 9
L logical type (logical )? Y n t f (? Is not initialized ).
M (Memo) characters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.