World Wind Java Development VI-Parse shape file (top)

Source: Internet
Author: User
Tags unsupported

Recently been busy with the Mentor project, a few days did not update, yesterday and today to study the next WWJ analysis shp file source code, now record, hope can help more people!

Previous blog: World Wind Java Development Five-read local shp file only how to load shp files, not related to the analysis of SHP files, our blog immediately after the previous blog, using WWJ to parse shp files. First look at the source package and related classes used, as shown in. The parse SHP file is used primarily for shapefiles (shapefiles file classes), Shapefilerecord (Shape file record classes), Dbaserecord classes, and Dbasefield (field classes)


1. Read Shapefiles file
It can be seen that there are six ways to instantiate a Shapefile object, taking two of them as an example to see the source code: (1)
/** * Opens a shapefiles from the InputStream, and inputstreams to their optional * resources. * <p/> * The source shapefiles May is accompanied optional streams to a index * resource stream, an attribute Resour Ce stream, and a projection resource * stream. If any of these streams is null or cannot is read for any * reason, the shapefiles opens without that information. * <p/> * This throws an exception if the Shapefiles ' s coordinate system is * unsupported. * * @param shpstream * The shapefile geometry file stream. * @param shxstream * The index file stream, can be null. * @param dbfstream * The attribute file stream, can be null. * @param prjstream * The projection file stream, can be null.             * @throws IllegalArgumentException * If the shapefile geometry stream <code>shpStream</code> is * Null.  * @throws Wwruntimeexception * If the shapefile cannot is opened for any reason, or if the *           Shapefiles ' s coordinate system is unsupported. */public shapefiles (InputStream shpstream, InputStream shxstream,inputstream dbfstream, InputStream prjStream) {This ( Shpstream, Shxstream, Dbfstream, prjstream, null);}
The input file stream corresponds to the. shp. dbf. prj file (2)
/** * Opens an shapefiles from a general source. The source type may be one of * the following: * <ul> * <li>{@link java.io.inputstream}</li> * <li&gt ; {@link java.net.url}</li> * <li>{@link file}</li> * <li>{@link String} containing a valid URL desc Ription or a file or * Resource name available on the classpath.</li> * </ul> * <p/> * the source Shapef Ile May is accompanied by an optional index file, * attribute file, and projection file. To is recognized by this shapefile, * accompanying files must is in the same logical folder as the Shapefile, * has the S Ame filename as the Shapefile, and has suffixes ". Shx", * ". dbf", and ". Prj" respectively. If any of these files does not exist, or * cannot is read for any reason, the shapefiles opens without that * information. * <p/> * This throws an exception if the Shapefiles ' s coordinate system is * unsupported, or if the Shapefile ' s coord Inate system is unsupported. * * @param SOURCE * The source of the Shapefile. * @throws IllegalArgumentException * If the source is a null or an empty string. * @throws Wwruntimeexception * If the shapefile cannot is opened for any reason. */public Shapefile (Object source) {This (source, null);}
This method of order gives the path of SHP files, but if only shp files, lack of shx and other files can not parse the shape file.According to the above two methods to instantiate a Shapefile object, the source code is as follows:
String Shpfilepath = "d:\\users\\wwj_data\\states.shp"; String Shxfilepath = "D:\\users\\wwj_data\\states.shx"; String Dbffilepath = "D:\\USERS\\WWJ_DATA\\STATES.DBF"; String Prjfilepath = "D:\\USERS\\WWJ_DATA\\STATES.PRJ"; Shapefiles shapefiles = new Shapefiles (Shpfilepath); System.out.println (Shapefile.getshapetype ());
Or:---------------------------------------------------------------------------------------
String Shpfilepath = "d:\\users\\wwj_data\\states.shp"; String Shxfilepath = "D:\\users\\wwj_data\\states.shx"; String Dbffilepath = "D:\\USERS\\WWJ_DATA\\STATES.DBF"; String Prjfilepath = "D:\\USERS\\WWJ_DATA\\STATES.PRJ"; InputStream shpinputstream = new FileInputStream (ShpFilePath) ; InputStream Shxinputstream = new FileInputStream (shxfilepath); InputStream dbfinputstream = new FileInputStream ( Dbffilepath); InputStream prjinputstream = new FileInputStream (prjfilepath);//Instantiate a shapefile-like shapefile shapefiles = new Shapefiles (Shpinputstream, Shxinputstream, Dbfinputstream, Prjinputstream); System.out.println (Shapefile.getshapetype ()); Shape type

The point to note here is that I used the shapefiles (Object source) method at the beginning, but the error: source is NULL, I do not know what the reason is, with this method can be, this can not be too tangled. 2. Get the attribute table information for shapefiles file in the Shapefile.java file, you can find the Attributefile field, which contains the properties information of the Shapefile file, but its permission is protected, Simply add a method in the original Java file to return the field values. After changing the source, re-export jar file overwrite reference.
protected dbasefileattributefile;
/** *  @ method Name: getattributestable; * @ Method Description:  gets the attribute table; * @ parameter: @return  * @ return type: dbasefile; * @ Creator: Running chicken silk; * @ created Room: 2014-12-1 pm 12:55:33; * @throws */public dbasefile getattributestable () {return this.attributefile;}
Once you get the property sheet, you first get basic information about the property sheet, such as the type of the shape file, the number of fields, and the number of records. Also output all field names
Get SHP attribute table Dbasefile Dbasefile = shapefile.getattributestable (); int fieldcount = Dbasefile.getnumberoffields (); Number of fields int recordscount = Dbasefile.getnumberofrecords (); Number of records SYSTEM.OUT.PRINTLN ("Number of fields:" + FieldCount); System.out.println ("Number of records:" + Recordscount); System.out.println (Shapefile.getshapetype ()); Shape type//Get field collection Dbasefield [] Dbasefields=dbasefile.getfields (); for (int i = 0; i < FieldCount; i++) {System.out.prin TLN (Dbasefields[i].getname ());}
The results of the operation are as follows:
Open the Properties sheet for the shp file under ArcMap and compare the output to the correct result. 3. Get field values
Parse shape file Try{while (Shapefile.hasnext ()) {Shapefilerecord record = Shapefile.nextrecord ();//Get a record Dbaserecord Dbaserecord = Record.getattributes (); Gets the property information for the record object[] values = Dbaserecord.getvalues (). ToArray ();//Gets the field value collection for (int i = 0; i < values.length; i++) {Syste M.out.println (Values[i].tostring ());} System.out.println ("------------------");}} catch (Exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Error parsing shapefiles file! ");} Finally{wwio.closestream (Shapefiles, Shpfilepath); Wwio.closestream (Shapefiles, Shxfilepath); Wwio.closestream (Shapefiles, Dbffilepath); Wwio.closestream (Shapefiles, Prjfilepath);}
The idea is simple: a shapefiles file, get a record, get property information for a record, and get a collection of field values. But there is one problem: Chinese text field values are not supportedThe results of the adjusted operation are as follows:

The property sheet under ArcMap is as follows:
By contrast, it is found that the field values are read, but the order is chaotic. There is no reason why, the next blog to solve the problem field values and fields do not correspond to the problem. The complete code is given below:
/** * @ Method Name: Shapefilereader; * @ Method Description: Read sh file; * @ parameter: @throws filenotfoundexception * @ return type: void; * @ Creator: running chicken silk; * @ creation Time: 2014-12-1 pm 12:50:11; * @throws */private void Shapefilereader () throws Filenotfoundexception{string Shpfilepath = "D:\\USERS\\WWJ_DATA\\STATES.SHP"; String Shxfilepath = "D:\\users\\wwj_data\\states.shx"; String Dbffilepath = "D:\\USERS\\WWJ_DATA\\STATES.DBF"; String Prjfilepath = "D:\\USERS\\WWJ_DATA\\STATES.PRJ"; InputStream shpinputstream = new FileInputStream (ShpFilePath) ; InputStream Shxinputstream = new FileInputStream (shxfilepath); InputStream dbfinputstream = new FileInputStream ( Dbffilepath); InputStream prjinputstream = new FileInputStream (prjfilepath);//Instantiate a shapefile-like shapefile shapefiles = new Shapefiles (Shpinputstream, Shxinputstream, Dbfinputstream, prjinputstream);//Get SHP property sheet Dbasefile dbasefile = Shapefile.getattributestable (); int fieldcount = Dbasefile.getnumberoffields (); Number of fields int recordscount = Dbasefile.getnumberofrecords (); Number of records SYSTEM.OUT.PRINTLN ("Number of fields:" + FieldCount); System.out.println ("Number of records:" + Recordscount); System.out.println (Shapefile.getshapetype ()); Shape type//Get field collection Dbasefield [] Dbasefields=dbasefile.getfields (); for (int i = 0; i < FieldCount; i++) {System.out.prin T (Dbasefields[i].getname () + "");} System.out.println ();//Parse Shape file Try{while (Shapefile.hasnext ()) {Shapefilerecord record = Shapefile.nextrecord (); Get a record Dbaserecord Dbaserecord = Record.getattributes (); Gets the property information for the record object[] values = Dbaserecord.getvalues (). ToArray ();//Gets the field value collection for (int i = 0; i < values.length; i++) {Syste M.out.print (values[i].tostring () + "");} System.out.println ("------------------");}} catch (Exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Error parsing shapefiles file! ");} Finally{wwio.closestream (Shapefiles, Shpfilepath); Wwio.closestream (Shapefiles, Shxfilepath); Wwio.closestream (Shapefiles, Dbffilepath); Wwio.closestream (Shapefiles, Prjfilepath);}}








World Wind Java Development VI-Parse shape file (top)

Related Article

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.