My app allows a user to choose a shapefile from their computer to load on top of a pre-selected basemap - currently just an ArcGIS Online map service added as a ArcGISDynamicMapServiceLayer. I'm using a FeatureLayer to store the shapefile. What is the best way to determine if the shapefile is of the same spatial reference as the basemap?
My goal is to send a message to the user when their shapefile projection doesn't match the basemap's projection to let them know that the layer will not be added to the map. I can't seem to retrieve the shapefile's spatial reference though when using the "getSpatialReference()" method, even after adding the FeatureLayer to the map. I have tried this with a shapefile that matches the basemap projection, and with a shapefile that doesn't match the basemap projection...the value of the FeatureLayer spatial reference is always null.
The following code:
map.getLayers().add(featureLayer);
System.out.println("Map projection is now: " + map.getSpatialReference());
System.out.println("Layer projection is now: " + featureLayer.getSpatialReference());
System.out.println("Basemap projection is now: " + basemap.getSpatialReference());
...just prints this output:
Map projection is now: SpatialReference [mWKID=102100, mWKT=PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere"...
Layer projection is now: null
Basemap projection is now: SpatialReference [mWKID=102100, mWKT=PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere"...
I'm just starting to learn the Java API so any help would be appreciated. I haven't found any good examples yet of how to deal with this situation.
Have you checked that the shapefile has a spatial reference assigned to it?
A shapefile can happily exist without ever being assigned a spatial reference. This would explain why you get null as a result of the getSpatialReference() call.
My two test shapefiles both have .prj files associated with them. One is simply geographic - GCS_WGS_1984 - and the other is WGS_1984_Web_Mercator_Auxiliary_Sphere (which matches the projection of my basemap).