Android Open Source - arcgis-runtime-samples-android Add C S V Activity






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2014 ESRI
 */*w ww .j  a v a2 s  . com*/
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the sample code usage restrictions document for further information.
 *
 */

package com.esri.arcgis.android.samples.addcsv2graphic;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.android.map.Callout;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleMarkerSymbol;

public class AddCSVActivity extends Activity {

  // ArcGIS Android elements
  MapView mMapView = null;
  ArcGISTiledMapServiceLayer basemapTileLayer;
  GraphicsLayer graphicsLayer = null;

  static final int DATE_DIALOG_ID = 0;
  int mYear = 2011;
  int mMonth = 11;
  int mDay = 1;

  ProgressDialog dialog;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Retrieve the map and initial extent from XML layout
    mMapView = (MapView) findViewById(R.id.map);
    /* create a @ArcGISTiledMapServiceLayer */
    basemapTileLayer = new ArcGISTiledMapServiceLayer(
        "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
    // add tiled basemap to Map View
    mMapView.addLayer(basemapTileLayer);
    // add graphics layer
    graphicsLayer = new GraphicsLayer();
    mMapView.addLayer(graphicsLayer);

    // enable wrap around
    mMapView.enableWrapAround(true);
    // attribute map
    mMapView.setEsriLogoVisible(true);

    MyOnSingleTapListener listener = new MyOnSingleTapListener(this);
    mMapView.setOnSingleTapListener(listener);

    Button datePicker = (Button) findViewById(R.id.datePicker1);

    datePicker.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // refactor to use FramentDialog
        showDialog(DATE_DIALOG_ID);
      }
    });

  }

  @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
      return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
    }
    return null;
  }

  private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
      mYear = year;
      mMonth = monthOfYear;
      mDay = dayOfMonth;

      String sday;
      String smonth;
      String syear;

      if (mDay < 10) {
        sday = "0" + mDay;
      } else {
        sday = "" + mDay;
      }

      mMonth++;
      if (mMonth < 10) {
        smonth = "0" + mMonth;
      } else {
        smonth = "" + mMonth;
      }

      syear = mYear - 2000 + "";

      String date = syear + smonth + sday;

      FetchEvents aef = new FetchEvents();
      aef.execute(date);

    }
  };

  void getCSVReport(String date) {
    /*
     * This is where the feeds are brought in for tornado reports, hail reports
     * and wind reports. The reports come in with X,Ys and comments. The
     * reported locations are added as features to a GraphicsLayer.
     */
    graphicsLayer.removeAll();
    getWindEvents(date);
    getHailEvents(date);
    getTornadoEvents(date);
  }

  private void getWindEvents(String date) {

    // Find all of the WIND events. Use URLConnection and read in each line
    // of the CSV file.
    try {
      URL wind = new URL("http://www.spc.noaa.gov/climo/reports/" + date + "_rpts_wind.csv");
      URLConnection windc = wind.openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(windc.getInputStream()));
      String inputLine;
      String[] columnNames = {};
      Point mapPnt;

      Graphic graphic;

      Map<String, Object> attr = new HashMap<String, Object>();

      ArrayList<Graphic> windEvents = new ArrayList<Graphic>();
      SimpleMarkerSymbol symbol = new SimpleMarkerSymbol(Color.BLUE, 20, SimpleMarkerSymbol.STYLE.DIAMOND);

      while ((inputLine = reader.readLine()) != null) {
        String[] values = inputLine.split(",");
        if (values[0].equals("Time")) {
          columnNames = values;
          continue;
        }

        double lat = Double.parseDouble(values[5]);
        double lon = Double.parseDouble(values[6]);
        mapPnt = new Point(lon, lat);

        // Create attributes to populate graphic.
        int index = 0;
        for (String key : columnNames) {
          attr.put(key, values[index]);
          index++;
        }

        graphic = new Graphic(mapPnt, symbol, attr);
        windEvents.add(graphic);
      }

      // Call this to add reports to the GraphicsLayer...
      addReports(windEvents);

      // Close the input stream...
      reader.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

  private void getHailEvents(String date) {

    // Find all of the HAIL events. Use URLConnection and read in each line
    // of the CSV file.
    try {
      URL hail = new URL("http://www.spc.noaa.gov/climo/reports/" + date + "_rpts_hail.csv");
      URLConnection hailConnect = hail.openConnection();
      BufferedReader in = new BufferedReader(new InputStreamReader(hailConnect.getInputStream()));
      String inputLine;
      String[] columnNames = {};
      Point pt;
      Graphic graphic;

      Map<String, Object> attr = new HashMap<String, Object>();

      ArrayList<Graphic> hailEvents = new ArrayList<Graphic>();
      SimpleMarkerSymbol symbol = new SimpleMarkerSymbol(Color.GREEN, 20, SimpleMarkerSymbol.STYLE.DIAMOND);

      while ((inputLine = in.readLine()) != null) {
        String[] values = inputLine.split(",");
        if (values[0].equals("Time")) {
          columnNames = values;
          continue;
        }

        double lat = Double.parseDouble(values[5]);
        double lon = Double.parseDouble(values[6]);

        pt = new Point(lon, lat);

        // Create attributes to populate graphic.
        int index = 0;
        for (String key : columnNames) {
          attr.put(key, values[index]);
          index++;
        }

        graphic = new Graphic(pt, symbol, attr);
        hailEvents.add(graphic);
      }

      // Call this to add reports to the GraphicsLayer...
      addReports(hailEvents);

      // Close the input stream...
      in.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private void getTornadoEvents(String date) {

    // Find all of the TORNADO events. Use URLConnection and read in each
    // line of the CSV file.
    try {
      URL tornado = new URL("http://www.spc.noaa.gov/climo/reports/" + date + "_rpts_torn.csv");
      URLConnection tornadoConnection = tornado.openConnection();
      BufferedReader in = new BufferedReader(new InputStreamReader(tornadoConnection.getInputStream()));
      String inputLine;
      String[] columnNames = {};
      Point pt;
      Graphic graphic;

      Map<String, Object> attr = new HashMap<String, Object>();

      ArrayList<Graphic> tornadoEvents = new ArrayList<Graphic>();

      SimpleMarkerSymbol symbol = new SimpleMarkerSymbol(Color.RED, 20, SimpleMarkerSymbol.STYLE.DIAMOND);

      while ((inputLine = in.readLine()) != null) {
        String[] values = inputLine.split(",");
        if (values[0].equals("Time")) {
          columnNames = values;
          continue;
        }

        double lat = Double.parseDouble(values[5]);
        double lon = Double.parseDouble(values[6]);

        pt = new Point(lon, lat);

        // Create attributes to populate graphic.
        int index = 0;
        for (String key : columnNames) {
          attr.put(key, values[index]);
          index++;
        }

        graphic = new Graphic(pt, symbol, attr);
        tornadoEvents.add(graphic);

      }

      addReports(tornadoEvents);
      in.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private void addReports(ArrayList<Graphic> graphics) {
    Graphic graphic;
    // if there is something to add, do it...
    if (graphics.size() > 0) {

      for (int i = 0; i < graphics.size(); i++) {
        graphic = graphics.get(i);
        graphicsLayer.addGraphic(graphic);

      }
    }

  }

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

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

  private class MyOnSingleTapListener implements OnSingleTapListener {

    // Here, we use a single tap to popup the attributes for a report...
    Context _ctx;
    private static final long serialVersionUID = 1L;

    public MyOnSingleTapListener(Context ctx) {
      _ctx = ctx;
    }

    @Override
    public void onSingleTap(float x, float y) {
      Callout mapCallout = mMapView.getCallout();
      mapCallout.hide();
      Point pnt = mMapView.toMapPoint(x, y);

      int[] grs = graphicsLayer.getGraphicIDs(x, y, 20);
      Log.d("Test", "Graphics number is " + grs.length);

      if (grs.length > 0) {
        Graphic g = graphicsLayer.getGraphic(grs[0]);
        Map<String, Object> atts = g.getAttributes();
        String text = "";
        for (int i = 0; i < atts.size(); i++) {
          text = text + atts.keySet().toArray()[i] + ": " + atts.values().toArray()[i] + "\n";
        }

        TextView tv = new TextView(_ctx);
        tv.setText(text);

        // Here, we populate the Callout with the attribute information
        // from the report.
        mapCallout.setOffset(0, -3);
        mapCallout.setCoordinates(pnt);
        mapCallout.setMaxHeight(350);
        mapCallout.setMaxWidth(900);
        mapCallout.setStyle(R.xml.mycalloutprefs);
        mapCallout.setContent(tv);

        mapCallout.show();
      }

    }
  }

  private class FetchEvents extends AsyncTask<String, Void, Void> {

    private ProgressDialog mProgDialog;

    // empty constructor
    public FetchEvents() {}

    @Override
    protected void onPreExecute() {
      // remove any previous callouts
      if (mMapView.getCallout().isShowing()) {
        mMapView.getCallout().hide();
      }
      // show progress dialog while searching for events
      mProgDialog = ProgressDialog.show(AddCSVActivity.this, "", "Fetching CVS, Please wait....", true);

    }

    @Override
    protected Void doInBackground(String... dateStrs) {

      if (dateStrs.length > 0) {
        getCSVReport(dateStrs[0]);
      }
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {

      // remove dialog
      if (mProgDialog.isShowing()) {
        mProgDialog.dismiss();
      }
      
      if (graphicsLayer != null && graphicsLayer.getNumberOfGraphics() == 0) {
        // update UI with notice that no results were found
        Toast.makeText(AddCSVActivity.this, "There were no storm reports on this date.", Toast.LENGTH_SHORT).show();
      } else {
        Toast.makeText(AddCSVActivity.this, "tap on an event to see it's information.", Toast.LENGTH_LONG).show();
      }

    }

  }

}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java