set spatial reference for a newly created raster data inside the java code

3233
5
Jump to solution
02-03-2016 08:38 PM
MohammdrezaFaraji
New Contributor

Hi,

I have a random image and I manually created a world file for it. So the world file contains the correct scales and UTM coordinates for the top-left corner of the image.

I am trying to add this as a raster data (Add raster data—ArcGIS Runtime SDK for Java | ArcGIS for Developers ) to the Bing map. The bing map's spatial reference is WGS_1984_Web_Mercator_Auxiliary_Sphere, but my UTM coordinates in the world file for the image are WGS_1984_UTM_Zone_12N. So I need to know how exactly I should assign the proper spatial reference in my java code to the image (imported as the raster data), so it gets displayed in the right location on the bing map.

I know how I can set a spatial reference for a raster data (that does not have one) inside the Arc Map, but I need to do this inside my Arc GIS Runtime java-based application.

Would you please help me on this?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
nita14
by
Occasional Contributor III

Hi Mohammad,

that is why I suggested creation of aux.xml file associated with your tiff file. Inside xml file you will see definition of raster's coordinate system. This file is automatically saved by ArcMap, so you can examine it 's structure, by making a sample raster in your coordinate system.

Regarding reprojection on the fly, According to the documentation it is not done automatically in ArcGIS Runtime. You need to test it. If the raster location is wrong, then I would try to use project method on RasterSource. See the sample; Add raster data—ArcGIS Runtime SDK for Java | ArcGIS for Developers

Regards,

Adam

View solution in original post

0 Kudos
5 Replies
nita14
by
Occasional Contributor III

Hi,

try this code to save world file:

File writerWld = new File();
BufferedWriter writerWld = new BufferedWriter(new FileWriter(wld))) {
Integer mapheight = map.exportMapImage().getHeight();
Integer mapwidth = map.exportMapImage().getWidth();
Double coordHeight = (map.getExtent().getUpperLeft().getY() - map.getExtent().getLowerLeft().getY());
Double coordWidth = (map.getExtent().getUpperRight().getX() - map.getExtent().getUpperLeft().getX());
writerWld.write(String.valueOf(coordWidth / mapwidth));
writerWld.newLine();
writerWld.write("0.000000");
writerWld.newLine();
writerWld.write("0.000000");
writerWld.newLine();
writerWld.write("-" + String.valueOf(coordHeight / mapheight));
writerWld.newLine();
writerWld.write(String.valueOf((map.getExtent().getUpperLeft().getX())));
writerWld.newLine();
writerWld.write(String.valueOf(map.getExtent().getUpperLeft().getY()));

I suggest you create aux.xml file as well.

Regards,

Adam

0 Kudos
MohammdrezaFaraji
New Contributor

Adam, thanks you for the answer.
However, I'm not sure if that's answering my question.
I know how to create the world file. The thing is, I have a BufferedImage which is created inside my java code, then I save/write this image on my computer. Meanwhile, Knowing the east/north scales of the image and easting/northing coordinates of its top-right corner, I create its world file by the below code:

PrintWriter writer = new PrintWriter("D:\\imageName.tfw", "UTF-8");

writer.println(NorthScale);

writer.println(0.0);

writer.println(0.0);

writer.println(EastScale);

writer.println(Easting);

writer.println(Northing);

writer.close();

This correctly creates a world file in the same location/name as my saved image. Now, if I want to load this image as  raster data on the right location on my base map inside my java code, I need to add its spatial reference (e.x., "WGS_1984_UTM_Zone_8N") so it can be displayed in the right location.

In ArcMap, this could be done by right click on the image in the Catalog and then click on Properties and finally editing the Spatial Reference inside "Raster Dataset Properties" window. But I need to do this in java and using java code and my question is how I should do this inside my Arc GIS Runtime java code!

Thanks,

Mohammad

0 Kudos
nita14
by
Occasional Contributor III

Hi Mohammad,

that is why I suggested creation of aux.xml file associated with your tiff file. Inside xml file you will see definition of raster's coordinate system. This file is automatically saved by ArcMap, so you can examine it 's structure, by making a sample raster in your coordinate system.

Regarding reprojection on the fly, According to the documentation it is not done automatically in ArcGIS Runtime. You need to test it. If the raster location is wrong, then I would try to use project method on RasterSource. See the sample; Add raster data—ArcGIS Runtime SDK for Java | ArcGIS for Developers

Regards,

Adam

0 Kudos
MohammdrezaFaraji
New Contributor

Adam, thank you very much . It worked!

Spatial reference of my original raster data was saving in the header of my image, so my aux.xml files didn't have spatial reference information, that's why I didn't know how an aux.xml file could contain the spatial reference as well.

0 Kudos
nita14
by
Occasional Contributor III

I am glad it works as expected. Unfortunately, GeoTIFF (without world file) is not yet supported as local raster source in ArcGIS Runtime. Hope this is going to change as Quartz will be released.

Regards,

Adam

0 Kudos