⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapefilereader.java

📁 geotools的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * Modidied JM 10/May/2000 Polygons created without 0,0 centroid so that GeoPolygon will auto calculate this
     * @see getShapeType
     * @return LineLayer a LineLayer containing the lines from this shapefile.
     */
    public Layer readPolygons(){
        //sort out ids...
        if(ids==null) getIds();
        
        
        //PolygonLayer map = new PolygonLayer();
        GeoPolygon gp;
        ShapefileShape polys[] = sf.getShapes();
        ShapePolygon poly;
        ShapePoint p[] = null;
        double px[],py[];
        int nPoints,nParts;
        int count = sf.getRecordCount();
        for(int i = 0;i<count;i++){
            poly = (ShapePolygon)polys[i];
            nPoints = poly.getNumPoints();
            nParts = poly.getNumParts();
            
            
            
            p = poly.getPartPoints(0);//main polygon
            // px = new double[p.length];
            // py = new double[p.length];
            
            // for(int k=0;k<p.length;k++){
            //   px[k]=p[k].getX();
            //   py[k]=p[k].getY();
            // }
            gp = new GeoPolygon(ids[i],p);
            if(nParts>1){
                if(DEBUG)System.out.println("SfR->Building multipart polygon");
                for(int j = 1;j<nParts;j++){
                    p = poly.getPartPoints(j);
                    
                    // px = new double[p.length];
                    //  py = new double[p.length];
                    
                    //  for(int k=0;k<p.length;k++){
                    //     px[k]=p[k].getX();
                    //     py[k]=p[k].getY();
                    //   }
                    //int k = gotIds?id[i].intValue():i+1;
                    gp.addSubPart((new GeoPolygon(ids[i],p)));
                    
                }
            }
            map.addGeoPolygon(gp);
            
            
            
        }
        return map;
        
    }
    
    /**
     * Reads the feature information from the shapefile
     * and produces a PointLayer for use in a Theme.
     * This will only work if the feature type is
     * point otherwise it will fail.
     * @see getShapeType
     * @return PointLayer a PointLayer containing the points from this shapefile.
     */
    public Layer readPoints(){
        //sort out ids...
        if(ids==null) getIds();
        
        
        //PointLayer map = new PointLayer();
        GeoPoint gp;
        ShapefileShape points[] = sf.getShapes();
        
        ShapePoint p = null;
        int count = sf.getRecordCount();
        for(int i = 0;i<count;i++){
            p = (ShapePoint)points[i];
            gp = new GeoPoint(ids[i], p.getX(),p.getY());
            map.addGeoPoint(gp);
        }
        return map;
        
    }
    
    
    
    
    class Loader extends Thread{
        public void run(){
            //System.out.println("Hello from sf threaded loader!");
            
            String sub="";
            if(name.indexOf('?')>=0){sub = name.substring(name.indexOf( '?' ),name.lastIndexOf('/'));}
            if(DEBUG)System.out.println("SfR->Sub "+sub);
            map.setStatus(map.LOADING);
            boolean shpZip = false, dbfZip = false;
            try{
                if(DEBUG)System.out.println("SfR->Looking for .zip version of "+name);
                String ext = ".zip";
                String noExt = base.getFile();
                if(DEBUG)System.out.println("SfR->No Ext "+noExt);
                
                if(noExt.toLowerCase().endsWith(".shp") || noExt.toLowerCase().endsWith(".zip")){
                    noExt = name.substring(0,name.length()-4);
                }
                if(DEBUG)System.out.println("SfR->No Ext "+noExt);
                
                //if(base.getFile().endsWith(".zip") || base.getFile().endsWith(".shp")){ext = "";}
                
                URL zipURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext);
                if(DEBUG)System.out.println("SfR->zip url = "+zipURL);
                URLConnection uc = zipURL.openConnection();
                uc.setUseCaches(false);
                uc.setDefaultUseCaches(false);
                if(DEBUG)System.out.println("SfR->Opening zis");
                int size = uc.getContentLength();
                
                if(size!=-1){
                    byte [] buf1 = new byte [size];
                    byte [] got = new byte[size/10];
                    InputStream is = uc.getInputStream();
                    int grab=0;
                    int total = 0;
                    
                    while ((grab = is.read(got)) > -1) {
                        
                        System.arraycopy(got, 0, buf1, total, grab);
                        total+=grab;
                        
                    }
                    got=null;
                    
                    
                    ByteArrayInputStream zipfile = new ByteArrayInputStream(buf1);
                    ZipInputStream zis = new ZipInputStream(zipfile);
                    ZipEntry ze;
                    
                    //while(zis.available()<uc.getContentLength()){System.out.println(zis.available());}
                    if(DEBUG)System.out.println("SfR->Looking for entries");
                    while(((ze= zis.getNextEntry())!=null)  && (!shpZip || !dbfZip)){
                        if(DEBUG)System.out.println("SfR->Found entry");
                        if(DEBUG)System.out.println(ze);
                        if(DEBUG)System.out.println("SfR->Entry: "+ze.getName());
                        if(DEBUG)System.out.println("SfR->Getting entry");
                        //ze = zis.getNextEntry();
                        
                        if(ze.getName().toLowerCase().endsWith(".shp")){
                            name = ze.getName();
                            if(DEBUG)System.out.println("SfR->Found .shp in zip file");
                            size = (int)ze.getSize();
                            byte [] buf = new byte [size];
                            byte [] in = new byte[size];
                            
                            int n=0;
                            int index = 0;
                            
                            while ((n = zis.read(in)) > -1) {
                                
                                System.arraycopy(in, 0, buf, index, n);
                                index+=n;
                                //System.out.println("Index :"+index+" n "+n+" of "+size);
                            }
                            
                            
                            zis.closeEntry();
                            ByteArrayInputStream bais = new ByteArrayInputStream(buf);
                            sf = new Shapefile(bais);
                            shpZip = true;
                        }
                        else {
                            if(ze.getName().toLowerCase().endsWith(".dbf")){
                                if(DEBUG)System.out.println("SfR->Found .dbf in zip file");
                                size = (int)ze.getSize();
                                byte [] buf = new byte [size];
                                byte [] in = new byte[size];
                                
                                int n;
                                int index = 0;
                                
                                while ((n = zis.read(in)) > -1) {
                                    
                                    System.arraycopy(in, 0, buf, index, n);
                                    index+=n;
                                    // System.out.println("Index :"+index+" n "+n+" of "+size);
                                }
                                
                                
                                zis.closeEntry();
                                ByteArrayInputStream bais = new ByteArrayInputStream(buf);
                                dbf = new Dbf(bais);
                                dbfZip = true;
                                //System.out.println("SfR->DBF done");
                            }
                        }
                    }
                    if(shpZip && dbfZip){
                        if(DEBUG)System.out.println("SfR->Zip file version used "+name);
                        populate();
                        //getIds();
                        return;
                    }
                    
                }
                
                //    zipURL.openStream();
            }
            catch(ZipException ze){System.err.println("SfR->zip read failed "+ze);}
            catch(IOException ie){System.err.println("SfR->zip read not possible, looking for .shp and .dbf ");}
            catch(Exception e2){System.err.println("SfR->General exception in zip read ");if(DEBUG){System.err.println(e2);e2.printStackTrace();}}
            if(DEBUG)System.out.println("SfR->Looking for files outside of zip file");
            try{
                String ext = ".shp";
                String noExt = base.getFile();
                if(noExt.toLowerCase().endsWith(".shp") || noExt.toLowerCase().endsWith(".zip")){
                    noExt = name.substring(0,name.length()-4);
                }
                
                URL shapeURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext);
                if(!shpZip){sf = new Shapefile(shapeURL);}
                
                ext = ".dbf";
                
                
                URL dbfURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext);
                if(!dbfZip){dbf = new Dbf(dbfURL);}
                URL shxURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext);
                sf.readIndex(shxURL.openStream());
                getIds();
                
            }catch(Exception e){map.setStatus(map.ERRORED);System.err.println("SfR->"+e);}
           populate();
        }
        
        protected void populate(){
            /*try{
                System.out.println("Silly psudo lag start");
            sleep((int)(Math.random()*50000));
            System.out.println("Silly psudo lag finish");
            }
            catch(Exception e){System.out.println("Error in wait "+e);};*/
             if(idCol==-1){
                guessIdCol();
                getIds();
            }
            
            
            switch(getShapeType()){
                case(Shapefile.POLYGON):
                    readPolygons();
                    break;
                case(Shapefile.ARC):
                    readLines();
                    break;
                case(Shapefile.POINT):
                    readPoints();
                    break;
                case(Shapefile.ARC_M):
                    readLinesM();
                    break;
                default:
                    map.setStatus(map.ERRORED);
            }
            map.setStatus(map.COMPLETED);
            sf=null;//is this needed any more? lets see.
        }
    }
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -