-1

I have a function that reads a set of coordinates and I get UTM coordinates. Later I convert these UTM coordinates to WGS84. No problems when this data comes from the set of 4 files (shp, .dbf, .shx and .prj).

This is my code to get the coordinates and convert them:

public Geometry getGeometryDistrict(String refCatDistrict, String shapeFileDistrict){

                Geometry union = null;

                File shapeFile = new File (shapeFileDistrict);

                try {

                   Query query = new Query();
                   query.setCoordinateSystem(DefaultGeographicCRS.WGS84);

                   Filter filter = CQL.toFilter("obj_co_id = '"+refCatDistrict+"'");
                   query.setFilter(filter);

                   FileDataStore storeDistrict = FileDataStoreFinder.getDataStore(shapeFile);

                   SimpleFeatureSource featureSource = storeDistrict.getFeatureSource();

                   SimpleFeatureCollection collection = featureSource.getFeatures(query);
                   SimpleFeature fDistrict = null;

                   ArrayList<Geometry> geometries = new ArrayList<>();

                   SimpleFeatureIterator itrDistrict = collection.features();

                   while(itrDistrict.hasNext()){

                      fDistrict = itrDistrict.next();

                      Geometry geomDistrict = (Geometry)fDistrict.getDefaultGeometry();
                      geometries.add(geomDistrict);

                   }

                   GeometryFactory factory = new GeometryFactory();

                   GeometryCollection geometryCollection = (GeometryCollection) factory.buildGeometry( geometries );

                   union = geometryCollection.union();

                   itrDistrict.close();
                   storeDistrict.dispose();

               }catch(IOException | NoSuchElementException | CQLException e){
                   Throwable cause = e.getCause();
                   System.out.println(cause);
               }

                return union;
            }

My problem is that these data do not always have the .prj file (needed to convert the coordinates), and only 3 files arrive (.dbf, .shp and .shx).

My Doubt is: Would there be any way to convert UTM coordinates to WGS84 without having the .prj file?

Mimmetico
  • 422
  • 9
  • 25
  • without a `.prj` file you just have a collection of random numbers – Ian Turton Jun 21 '19 at 06:50
  • OP knows the coordinates are in UTM and wants to convert to WGS84. It is not explicit to me if he wants to convert a shapefile in UTM to WGS84, even without .prj file, or an arbitrary pair of coordinates. – LuisTavares Jun 21 '19 at 11:19

2 Answers2

1

You cannot convert UTM coordinates to WGS84 without prj. You either need Central Meridian or UTM Zone for the conversion.

However there is a work around. You can predict the UTM zone using World UTM zone . This can be done if you know the exact area. Then using trial and error you can convert and check for nearby 2-3 zones.

Surabhi Mundra
  • 377
  • 1
  • 12
  • Link for creating a .prj https://get.google.com/albumarchive/112948859575572152309/album/AF1QipMl5HnGkyANUeKny9_fd8Oul_2LyZAETDjbDol2?feat=directlink&authKey=COTl5d7lubmyxAE – Surabhi Mundra Jun 21 '19 at 04:46
1

I found this solution and apparently it works fine:

 try{      

   CoordinateReferenceSystem sourceutm = CRS.decode(String.format("AUTO2:42001,%s,%s", -3.691406, 40.403431), true);
   CoordinateReferenceSystem targetlatlong = CRS.decode("EPSG:4326", true);

   MathTransform transform = CRS.findMathTransform(sourceutm, targetlatlong, false);
   Geometry union = JTS.transform( geometryCollection, transform);

   System.out.println(union.toString());

  }catch(MismatchedDimensionException | FactoryException | TransformException e){
         throw new RuntimeException(e);
       }    

This inside the while, of course. And returns me a degree coordinates from a UTM. No .pjr needed.

Mimmetico
  • 422
  • 9
  • 25
  • 1
    You are passing the CRS. That itself is the content of prj! If you know the CRS of your file then obviously you do not need a prj – Surabhi Mundra Jun 28 '19 at 05:30