Showing posts with label GIS Map. Show all posts
Showing posts with label GIS Map. Show all posts

Thursday, May 26, 2016

Arc GIS Mapping Example with Source code


1)ArcGisActivity.java

package arc.gis;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.ags.ArcGISFeatureLayer.MODE;
import com.esri.android.map.ags.ArcGISFeatureLayer.Options;
import com.esri.android.map.ags.ArcGISFeatureLayer.SELECTION_METHOD;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.tasks.SpatialRelationship;
import com.esri.core.tasks.ags.query.Query;

public class ArcGisActivity extends Activity {

MapView mMapView ;
ArcGISFeatureLayer fLayer = null;
GraphicsLayer gLayer = null;
SimpleFillSymbol sfs;
CallbackListener<FeatureSet> callback = new CallbackListener<FeatureSet>() {
public void onCallback(FeatureSet fSet) {
System.out.println("onCallback"+fSet);
}

public void onError(Throwable arg0) {
gLayer.removeAll();
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mMapView = (MapView)findViewById(R.id.map);
ESRIShapeLayer graphicsLayer = new ESRIShapeLayer("/mnt/sdcard/sample.shp",mMapView.getExtent());
Options o = new Options();
o.mode = MODE.ONDEMAND;
o.outFields = new String[] { "FIELD_KID", "APPROXACRE", "FIELD_NAME",
"STATUS", "PROD_GAS", "PROD_OIL", "ACTIVEPROD", "CUMM_OIL",
"MAXOILWELL", "LASTOILPRO", "LASTOILWEL", "LASTODATE",
"CUMM_GAS", "MAXGASWELL", "LASTGASPRO", "LASTGASWEL",
"LASTGDATE", "AVGDEPTH", "AVGDEPTHSL", "FIELD_TYPE",
        "FIELD_KIDN"
};
gLayer=graphicsLayer.getLayer();
mMapView.addLayer(gLayer);

mMapView.setOnSingleTapListener(new OnSingleTapListener() {

private static final long serialVersionUID = 1L;
public void onSingleTap(float x, float y) {
Toast.makeText(getApplicationContext(), "You have touched at X-Y points::"+x+" "+y, Toast.LENGTH_LONG).show();

}
});


/* mMapView.setExtent(new Envelope(-10868502.895856911, 4470034.144641369,
-10837928.084542884, 4492965.25312689), 0);
ArcGISTiledMapServiceLayer tms = new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
mMapView.addLayer(tms);*/

fLayer = new ArcGISFeatureLayer(
"http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1",
o);
SimpleFillSymbol fiedldsSelectionSymbol = new SimpleFillSymbol(Color.MAGENTA);
fiedldsSelectionSymbol.setOutline(new SimpleLineSymbol(Color.YELLOW, 2));
fLayer.setSelectionSymbol(fiedldsSelectionSymbol);

mMapView.addLayer(fLayer);

// selection box
gLayer = new GraphicsLayer();
sfs = new SimpleFillSymbol(Color.BLACK);
sfs.setOutline(new SimpleLineSymbol(Color.RED, 2));
sfs.setAlpha(100);

mMapView.addLayer(gLayer);
//setContentView(map);

MyTouchListener touchListener = new MyTouchListener(this, mMapView);
mMapView.setOnTouchListener(touchListener);

Toast.makeText(this, "Press down to start let go the stop",Toast.LENGTH_SHORT).show();
}

class MyTouchListener extends MapOnTouchListener {

Graphic g;
// first point clicked on the map
Point p0 = null;
int uid = -1;

public MyTouchListener(Context arg0, MapView arg1) {
super(arg0, arg1);
}

public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
if (uid == -1) { // first time
g = new Graphic(null, sfs);
p0 = mMapView.toMapPoint(from.getX(), from.getY());
uid = gLayer.addGraphic(g);

} else {

Point p2 = mMapView.toMapPoint(new Point(to.getX(), to.getY()));
Envelope envelope = new Envelope();
envelope.merge(p0);
envelope.merge(p2);
gLayer.updateGraphic(uid, envelope);

}

return true;

}
public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {

if (uid != -1) {
g = gLayer.getGraphic(uid);
if (g!= null && g.getGeometry() != null) {
fLayer.clearSelection();
Query q = new Query();
// optional
q.setWhere("PROD_GAS='Yes'");
q.setReturnGeometry(true);
q.setInSpatialReference(mMapView.getSpatialReference());
q.setGeometry(g.getGeometry());
q.setSpatialRelationship(SpatialRelationship.INTERSECTS);

fLayer.selectFeatures(q, SELECTION_METHOD.NEW, callback);
}
gLayer.removeAll();

}

p0 = null;
// Resets it
uid = -1;
return true;

}

}

@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}

@Override
public Object onRetainNonConfigurationInstance() {
return mMapView.retainState();
}
}

2) ESRIShapeLayer.java

package arc.gis;

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.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
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 {

private GraphicsLayer graphicsLayer = null;// = new GraphicsLayer();
private String shapeFileName = null;
private Polygon polygon = null;// = new Polygon();
private Point polygonVertex = null;// = new Point();
@SuppressWarnings("unused")
private int polygonCount = 0;


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

public ESRIShapeLayer(String shapeFileName, Polygon pol) {
this.shapeFileName = shapeFileName;
SpatialReference lSR = SpatialReference.create(2093);  //26192   2093
Envelope lEnvolope = getSHPEnvelope(shapeFileName);

graphicsLayer = new GraphicsLayer(lSR,lEnvolope);
polygon = new Polygon();
polygonVertex = new Point();
//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();
}
if (esriShapeFile.getShapeType() == ShapeFile.SHAPE_TYPE_POLYLINE) {
/**
* Have not figured this out yet.
*/
}
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[i];

//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++) {
// System.out.println("poly.getX(j):"+poly.getX(j)+" poly.getY(j):"+poly.getY(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.
*/

//polygon.calculateArea2D();
graphicsLayer.addGraphic(new Graphic(polygon, simpleFillSymbol));
esriPolygonRecord = (ESRIPolygonRecord) esriShapeFile.getNextRecord();
}
//esriShapeFile.close();
// System.out.println("Polygon Count: " + polygonCount);
}

esriShapeFile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return graphicsLayer;
}

private Envelope getSHPEnvelope(String shapName) {

double xMin = 120.0;        // 220.0
double xMax = 150.0;         //250.0
double yMin = 150.0;         //250.0
double yMax = -150.0;        //-250.0

/*double xMin = -130.0;
double xMax = -114.0;
double yMin = 31.0;
double yMax = 41.5;*/
/*try {
File file = new File(shapName);
ShapeFile esriShapeFile = new ShapeFile(file);
ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();

}catch (Exception e) {
e.printStackTrace();
}*/

Envelope envelop = new Envelope();
envelop.setCoords(xMin, xMax, yMin, yMax);

/*Envelope env = new Envelope();
Envelope NewEnv = new Envelope();
for (int i:graphicsLayerLines.getGraphicIDs()){
Polyline p = (Polyline)graphicsLayerLines.getGraphic(i).getGeometry();
   p.queryEnvelope(env);
   NewEnv.merge(env);
}
for (int i:graphicsLayerPoints.getGraphicIDs()){
 Point p = (Point)graphicsLayerPoints.getGraphic(i).getGeometry();
 p.queryEnvelope(env);
 NewEnv.merge(env);
}*/

return envelop;
}
}

3) main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".ArcGisActivity" >

    <com.esri.android.map.MapView
       
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        initExtent = "-1.3296373526814876E7 3930962.41823043 -1.2807176545789773E7 4201243.7502468005">
    </com.esri.android.map.MapView>

</LinearLayout>

4)<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="arc.gis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" android:maxSdkVersion="17"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

   

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ArcGisActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download the library from the website arcggismappming.........