Select to view content in your preferred language

Find task for a local MPK

3556
5
Jump to solution
12-13-2013 03:21 AM
IbrahimHussein
Deactivated User
Hello, I am testing out the findApp class, I took a look at the sample and I am trying to test it out with a local mpk.

The majority of the code below is from the javaSDK. I made a few changes such as adding a local tpk and mpk. and set the search for parameters by name field (I also try searching just by layerID to see if that worked.) And nothing I tried works. this is whats in the Console everytime it searches. the code is in the 2nd post, as it couldnt fit in a single post.

com.esri.core.io.EsriSecurityException: EsriSecurityException[code = -10001 msg = User credentials were invalid. Please supply a valid username and password in order to access the secure service.
 at com.esri.core.internal.io.handler.c.a(Unknown Source)
 at com.esri.core.internal.io.handler.r.a(Unknown Source)
 at com.esri.core.internal.io.handler.r.a(Unknown Source)
 at com.esri.core.internal.tasks.d.getServiceCredentials(Unknown Source)
 at com.esri.core.internal.tasks.ags.a.a.a(Unknown Source)
 at com.esri.core.tasks.ags.find.FindTask.execute(Unknown Source)
 at project01.FindApp.onFind(FindApp.java:119)
 at project01.FindApp.access$1(FindApp.java:94)
 at project01.FindApp$2.actionPerformed(FindApp.java:203)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)


EDIT - at project01.FindApp.onFind(FindApp.java:119 --------- findResults = task.execute(params);
so I am guessing something is wrong with the parameters, I added all layers just to see if I could find anything. and still the same issue.

Any help or direction would be helpful, thanks.
0 Kudos
1 Solution

Accepted Solutions
ColinAnderson1
Esri Contributor
You would need to do...

LocalMapService locMapService = new LocalMapService(Path to mpk);
locMapService.start(); // start the local service
String url = locMapService.getUrlMapService(); // this is the URL of the maps service
// to do find task you need url to a layer
url += "/0"; // add the ID of a layer
FindTask findTask = new FindTask(url); // set up your find task

If you look in the ConsoleApp sample (com.esri.client.samples.localserver) you will see this work flow.

A simpler work flow for you could be to use ArcGISLocalFeatureLayer which will create a local server based feature layer and if you call getUrl() that url will be the complete URL including layer ID. LocalMapService and LocalFeatureService are only really intended for when you want to create local services with multiple layers. ArcGISLocalFeatureLayer can be added directly to the map which will take care of any initialisation for you.

View solution in original post

0 Kudos
5 Replies
IbrahimHussein
Deactivated User
I had to remove the static and public methods to fit the post, but they were unchanged from the SDK. I can post them if needed. thanks

// Copyright 2013 ESRI

/***
 * This sample demonstrates executing a FindTask, adding the results to
 *  a table, and zooming to a selected result.
 */
public class FindApp {
   
    // UI related
    JTextField findText = new JTextField("Colorado");
    DefaultTableModel tblModelStateInfo;

    // JMap
    private JMap map;

    // our graphics layer for highlighted geometries
    private GraphicsLayer graphicsLayer;

    // for zooming to a buffered geometry
    private static final double BUFFER_DISTANCE = 500000; // 500 km

    // results of the Find task execution
    List<FindResult> findResults = null;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------
    public FindApp() {
    }

    // ------------------------------------------------------------------------
    // Core functionality
    // ------------------------------------------------------------------------
    /**
     * This method creates and executes the FindTask to find the input text
     *
     */
    private void onFind() {
        tblModelStateInfo.setRowCount(0);

        // return if input text is not valid
        if (findText.getText() == null || findText.getText().isEmpty()) {
            return;
        }

        // -----------------------------------------------------------------------------------------
        // Find task to find value 'Colorado' in all attributes of dynamic
        // service
        // -----------------------------------------------------------------------------------------
        // create a Find task.
        FindParameters params = new FindParameters();
        params.setSearchText(findText.getText());
        params.setLayerIds(new int[] { 0, 1, 2,3,4,5,6,7,8});
        params.setSearchFields(new String[]{"Streets"});//search only the named field
        params.setOutputSpatialRef(map.getSpatialReference());

        // execute the query
        FindTask task = new FindTask("C:\\mpk.mpk");
        try {
            findResults = task.execute(params);

            if (findResults == null){
                return;
            }

            for (FindResult result : findResults) {
                tblModelStateInfo.addRow(new Object[] {
                        result.getAttributes().get(result.getDisplayFieldName()),
                        Integer.valueOf(result.getLayerId()), result.getLayerName(),
                        result.getFoundFieldName(), result.getValue() });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   

    // ------------------------------------------------------------------------
    // Private methods
    // ------------------------------------------------------------------------
    /**
     * Creates a window.
     *
     * @return a window.
     */
    private JFrame createWindow() {
        JFrame window = new JFrame("Find Application");
        window.setBounds(100, 100, 1000, 700);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().setLayout(new BorderLayout(0, 0));
        window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                super.windowClosing(windowEvent);
                map.dispose();
            }
        });
        return window;
    }

    /**
     * Creates a map.
     *
     * @return a map.
     */
    private JMap createMap() throws Exception {
        final JMap jMap = new JMap();
        // -----------------------------------------------------------------------------------------
        // Base Layer - with US topology, focus on U.S by default
        // -----------------------------------------------------------------------------------------

            final ArcGISLocalTiledLayer baseLayer = new ArcGISLocalTiledLayer(
                "C:\\tpk.tpk"); //local tpk added here
        jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
        
        jMap.getLayers().add(baseLayer);

        // -----------------------------------------------------------------------------------------
        // Dynamic Layer - US Cities and States that we will search
        // -----------------------------------------------------------------------------------------
                final ArcGISLocalDynamicMapServiceLayer layer = new ArcGISLocalDynamicMapServiceLayer(
                "C:\\mpk.mpk"); // I added the local mpk here
        layer.setOpacity(0.6f);
        jMap.getLayers().add(layer);

        // -----------------------------------------------------------------------------------------
        // Graphics Layer - to highlight a selected feature
        // -----------------------------------------------------------------------------------------
        graphicsLayer = new GraphicsLayer();
        jMap.getLayers().add(graphicsLayer);

        return jMap;
    }

    /**
     * Display a geometry on the map for the selected table row
     *
     * @param geometryType
     * @param geometry
     */
    private void highlightGeometry(Type geometryType, Geometry geometry) {
        if (geometryType == null || geometry == null) {
            return;
        }

        // -----------------------------------------------------------------------------------------
        // Add a graphic for the selected feature to the graphics layer
        // -----------------------------------------------------------------------------------------
        graphicsLayer.removeAll();
        switch (geometryType) {
        case POINT:
            graphicsLayer.addGraphic(new Graphic(geometry,
                    new SimpleMarkerSymbol(Color.RED, 8,
                            SimpleMarkerSymbol.Style.DIAMOND)));
            break;
        case POLYLINE:
            graphicsLayer.addGraphic(new Graphic(geometry,
                    new SimpleLineSymbol(Color.RED, 4,
                            SimpleLineSymbol.Style.SOLID)));
            break;
        case POLYGON:
            graphicsLayer.addGraphic(new Graphic(geometry,
                    new SimpleFillSymbol(Color.RED)));
            break;
          default:
            break;
        }

        // -----------------------------------------------------------------------------------------
        // Zoom to the highlighted graphic
        // -----------------------------------------------------------------------------------------
        Geometry geometryForZoom = GeometryEngine.buffer(geometry,
                map.getSpatialReference(), BUFFER_DISTANCE, map
                        .getSpatialReference().getUnit());
        map.zoomTo(geometryForZoom);
    }

}
0 Kudos
ColinAnderson1
Esri Contributor
You can't run the FindTask against a path to your mpk - FindTask task = new FindTask("C:\\mpk.mpk") - you need to supply a URL.

To get the URL you will need to create a local layer from your MPK and then use the URL of that in the constructor. If you look at the samples in com.esri.client.samples.localserver you will see how to create local layers. You can then use getURL() on the local layer to get a URL for your find task.
0 Kudos
IbrahimHussein
Deactivated User
You can't run the FindTask against a path to your mpk - FindTask task = new FindTask("C:\\mpk.mpk") - you need to supply a URL.

To get the URL you will need to create a local layer from your MPK and then use the URL of that in the constructor. If you look at the samples in com.esri.client.samples.localserver you will see how to create local layers. You can then use getURL() on the local layer to get a URL for your find task.


Thanks, I was just about to give up. ill look into that class.

Since I want the server to start automatically and know which service to run, can I just do
LocalMapService locMapService = new LocalMapService(Path to mpk);
String url = LocMapService.getUrl(); ???

Cant seem find find any documentation on the issue, sample isnt really that clear.
0 Kudos
ColinAnderson1
Esri Contributor
You would need to do...

LocalMapService locMapService = new LocalMapService(Path to mpk);
locMapService.start(); // start the local service
String url = locMapService.getUrlMapService(); // this is the URL of the maps service
// to do find task you need url to a layer
url += "/0"; // add the ID of a layer
FindTask findTask = new FindTask(url); // set up your find task

If you look in the ConsoleApp sample (com.esri.client.samples.localserver) you will see this work flow.

A simpler work flow for you could be to use ArcGISLocalFeatureLayer which will create a local server based feature layer and if you call getUrl() that url will be the complete URL including layer ID. LocalMapService and LocalFeatureService are only really intended for when you want to create local services with multiple layers. ArcGISLocalFeatureLayer can be added directly to the map which will take care of any initialisation for you.
0 Kudos
IbrahimHussein
Deactivated User
You would need to do...

LocalMapService locMapService = new LocalMapService(Path to mpk);
locMapService.start(); // start the local service
String url = locMapService.getUrlMapService(); // this is the URL of the maps service
// to do find task you need url to a layer
url += "/0"; // add the ID of a layer
FindTask findTask = new FindTask(url); // set up your find task

If you look in the ConsoleApp sample (com.esri.client.samples.localserver) you will see this work flow.

A simpler work flow for you could be to use ArcGISLocalFeatureLayer which will create a local server based feature layer and if you call getUrl() that url will be the complete URL including layer ID. LocalMapService and LocalFeatureService are only really intended for when you want to create local services with multiple layers. ArcGISLocalFeatureLayer can be added directly to the map which will take care of any initialisation for you.


Thanks dude, ill check it em both out.


edit - Got it working. just a little hint for those who encounter the problem. Make sure you stop the service [locMapService.Stop()], since you are only allowed 3 instances of a local server (after its doing finding of course)
0 Kudos