How to open shape file (*.shp) from SD Card ?

9168
20
04-25-2012 08:10 AM
mrphammrpham
New Contributor
Hi all !

How to open shape file (*.shp) from SD Card ?

Thank you all !
0 Kudos
20 Replies
LukeCatania
New Contributor III
static public GraphicsLayer SHPtoPOINT(String shpfile) {
  SpatialReference lSR = SpatialReference.create(26192);
  Envelope lEnvolope = getSHPEnvelope(shpfile);//to create an extent for your graphics layer
  GraphicsLayer graphicLayer = new GraphicsLayer(lSR, lEnvolope);

  try {
   File file = new File(shpfile);
   ShapeFile shp = new ShapeFile(file);
   ESRIPointRecord e = (ESRIPointRecord) shp.getNextRecord();
   SimpleMarkerSymbol c_point = new SimpleMarkerSymbol(Color.BLACK, 1,
     STYLE.CIRCLE);
   while (e != null) {
    graphicLayer.addGraphic(new Graphic(new Point(e.getX(), e
      .getY()), c_point));
    e = (ESRIPointRecord) shp.getNextRecord();
   }
   shp.close();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  return graphicLayer;
}



I added ability to read polygon data, but it makes the display extremely slow when zooming.  I imagine it has to do with redrawing all the polygons.  When just looking at Point shape files, it does not seem to have this issue.  Anyone have any ideas how to speed it up.  Also just loading the data makes the app take longer to start.  Here is what I added.  Still need to work on polylines.

import java.io.File;
import java.io.IOException;

import android.graphics.Color;

import com.bbn.openmap.layer.shape.ESRIPointRecord;
import com.bbn.openmap.layer.shape.ESRIPoly;
import com.bbn.openmap.layer.shape.ESRIPolygonRecord;
import com.bbn.openmap.layer.shape.ShapeFile;
import com.esri.android.map.GraphicsLayer;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;

public class ESRIShapeLayer {

    GraphicsLayer    graphicsLayer    = new GraphicsLayer();
    String           shapeFileName    = null;
    Polygon          polygon          = new Polygon();
    Point            polygonVertex    = new Point();
    int              polygonCount     = 0;

    /**
     * Save applications's state
     */
    ApplicationState applicationState = null;

    public ESRIShapeLayer(String shapeFileName, ApplicationState applicationState) {
        this.shapeFileName = shapeFileName;
        this.applicationState = applicationState;
        readShapeFile();
    }

    public GraphicsLayer getLayer() {
        return graphicsLayer;
    }

    public GraphicsLayer readShapeFile() {
        try {
            File file = new File(shapeFileName);
            ShapeFile esriShapeFile = new ShapeFile(file);
            ESRIPoly[] polygons;

            System.out.println("Shape Type: " + esriShapeFile.getShapeType());

            if (esriShapeFile.getShapeType() == ShapeFile.SHAPE_TYPE_POINT) {
                ESRIPointRecord esriPointRecord = (ESRIPointRecord) esriShapeFile.getNextRecord();
                SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(Color.BLUE, 10, STYLE.CIRCLE);

                while (esriPointRecord != null) {
                    graphicsLayer.addGraphic(new Graphic(
                        new Point(esriPointRecord.getX(), esriPointRecord.getY()),
                        simpleMarkerSymbol));
                    esriPointRecord = (ESRIPointRecord) esriShapeFile.getNextRecord();
                }
                esriShapeFile.close();
            } else if (esriShapeFile.getShapeType() == ShapeFile.SHAPE_TYPE_POLYLINE) {
                /**
                 * Have not figured this out yet.
                 */
            } else if (esriShapeFile.getShapeType() == ShapeFile.SHAPE_TYPE_POLYGON) {

                ESRIPolygonRecord esriPolygonRecord = (ESRIPolygonRecord) esriShapeFile.getNextRecord();
                SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol(Color.BLUE);

                while (esriPolygonRecord != null) {
                    polygons = esriPolygonRecord.polygons;

                    System.out.println("Number of Polygons Per Record[" + polygons.length + "]: "
                        + esriPolygonRecord.getRecordNumber());

                    for (int i = 0; i < polygons.length; i++) {
                        polygonCount++;

                        ESRIPoly.ESRIFloatPoly poly = (ESRIPoly.ESRIFloatPoly) polygons;

                        System.out.println("Polygon Point Count[: " + esriPolygonRecord.getRecordNumber() + "]: "
                            + poly.nPoints);

                        polygonVertex = new Point(poly.getX(0), poly.getY(0));
                        polygon = new Polygon();
                        polygon.startPath(polygonVertex);

                        for (int j = 1; j < poly.nPoints; j++) {
                            polygonVertex = new Point(poly.getX(j), poly.getY(j));
                            polygon.lineTo(polygonVertex);
                        }
                    }
                    /**
                     * Move inside loop above otherwise it is only displaying one of the polygons per record.
                     */
                    graphicsLayer.addGraphic(new Graphic(polygon, simpleFillSymbol));
                    esriPolygonRecord = (ESRIPolygonRecord) esriShapeFile.getNextRecord();
                }
                esriShapeFile.close();
                // System.out.println("Polygon Count: " + polygonCount);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return graphicsLayer;
    }
}
0 Kudos
VivekRane
New Contributor
where to store the shapefiles in sd card (location path)?
0 Kudos
LukeCatania
New Contributor III
where to store the shapefiles in sd card (location path)?


You can store it where ever you want.  Just create folder on your SD Card.  Usually you create a directory with the same name as your App and then you can create a directory under that.  I call my subdirectory "Features", then I pass the shapefile name with full path to it to the constructor.
0 Kudos
VivekRane
New Contributor
Is it possible to get attribute from shapefiles and show shapefiles on bing map?
0 Kudos
SolidSmoke
New Contributor
I ve tried to get the attribute using the following way



     File dbf = new File(Environment.getExternalStorageDirectory()
     + "/islreali/TabaYeud.dbf");
     File shape = new File(Environment.getExternalStorageDirectory()
     + "/islreali/TabaYeud.shp");
     File shx = new File(Environment.getExternalStorageDirectory()
     + "/islreali/TabaYeud.shx");

     try {
            URL dbfUrl = dbf.toURI().toURL();
     EsriPlugIn epi = new EsriPlugIn("name", dbfUrl, shape.toURI().toURL(), shx.toURI().toURL());
            EsriGraphicList eGList = epi.getEsriGraphicList();
     Map<Object, Object> attr = eGList.getAttributes();
  } catch (Exception e) {
       e.printStackTrace();
  }



In the above code, it throws an error when it reach the place wher we use EsriPlugIn. I get Noclassdef error. I think this is not supported in android as read from the below link
http://openmap.bbn.com/mailArchives/openmap-users/2011-03/6671.html


Cud anyone advise me how to read the shapefile attributes by using some other method of openmap or some other api.
0 Kudos
sekharsamanta
New Contributor
static public GraphicsLayer SHPtoPOINT(String shpfile) {
  SpatialReference lSR = SpatialReference.create(26192);
  Envelope lEnvolope = getSHPEnvelope(shpfile);//to create an extent for your graphics layer
  GraphicsLayer graphicLayer = new GraphicsLayer(lSR, lEnvolope);

  try {
   File file = new File(shpfile);
   ShapeFile shp = new ShapeFile(file);
   ESRIPointRecord e = (ESRIPointRecord) shp.getNextRecord();
   SimpleMarkerSymbol c_point = new SimpleMarkerSymbol(Color.BLACK, 1,
     STYLE.CIRCLE);
   while (e != null) {
    graphicLayer.addGraphic(new Graphic(new Point(e.getX(), e
      .getY()), c_point));
    e = (ESRIPointRecord) shp.getNextRecord();
   }
   shp.close();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  return graphicLayer;
}


"From where will I get SimpleMarkerSymbol class. I downloaded openmap 5.0 and the jars from there but could not find SimpleMarkerSymbol , Envelope , SpatialReference  classes?"
0 Kudos
LukeCatania
New Contributor III
"From where will I get SimpleMarkerSymbol class. I downloaded openmap 5.0 and the jars from there but could not find SimpleMarkerSymbol , Envelope , SpatialReference  classes?"


Those are all part of the ArcGIS for Android API.
0 Kudos
AnsarRauf
New Contributor
I want to open polyline shapefile from SD card, can anyone guide on how to convert it into graphics layer? The code above in the thread is for the point and polygon shapefile.
0 Kudos
canwang
New Contributor
Download code from:

http://www.openmap.org/

and then point to the jar files in the "lib" directory through the Java Build Path Libraries Tab in  Eclsipse


Can you tell me how to use the openmap after add the lib?
or show me a sample/tutorial of open shape file from SD card by openmap.
0 Kudos
LukeCatania
New Contributor III
Can you tell me how to use the openmap after add the lib?
or show me a sample/tutorial of open shape file from SD card by openmap.


That's what the code I posted in this thread shows.  It uses

com.bbn.openmap.layer.shape.ESRIPointRecord;
com.bbn.openmap.layer.shape.ESRIPoly;
com.bbn.openmap.layer.shape.ESRIPolygonRecord;
com.bbn.openmap.layer.shape.ShapeFile;

from openmap.
0 Kudos