SoFunction
Updated on 2025-03-04

Is there a certain latitude and longitude of Java in a genjson file?

Is a certain latitude and longitude of Java in a genjson file?

GeoJSONIt is a format that encodes various geospatial information data exchange format based on JavaScript object notation (JSON).

If we want to use whether a certain latitude and longitude is in a certain area, we first need to use the genjson file, because the province map is an irregular figure, and we need to use strokes to determine whether it is inside the area.

We need to parse the genjson file first

Then, when comparing with latitude and longitude, you need to import the GenJSON library in advance

      <dependency>
            <groupId></groupId>
            <artifactId>gt-geojson</artifactId>
            <version>23.5</version>
        </dependency>

Create an object

import ;

@Data
public class LatLng {

    double lat;
    double lng;


    public LatLng(double lat,double lng){
         = lat;
         = lng;
    }
}

Tools

import ;
import ;

import ;
import ;
import ;
import ;

public class GeojsonUtils {


    /**
      *
      * @param path genjson location
      * @param lat
      * @param lng Longitude
      * @return
      */
    public static Boolean getGenjsonPosition(String path,Double lat,Double lng){
        try{
            String jsonString = new String(((path)));
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = (jsonString);
            JsonNode coordinatesNode = ("features").get(0).path("geometry").path("coordinates").get(0);
            List&lt;LatLng&gt; coordinatesList = new ArrayList&lt;&gt;();

            for (JsonNode coordinate : coordinatesNode) {
                double rlng = (0).asDouble();
                double rlat = (1).asDouble();
                LatLng point = new LatLng(rlat,rlng);
                (point);
            }

            LatLng targetPoint = new LatLng(lat, lng);
            boolean isInArea = false;
            int j = () - 1;

            for (int i = 0; i &lt; (); i++) {
                LatLng point1 = (i);
                LatLng point2 = (j);

                if ((() &lt; () &amp;&amp; () &gt;= ()
                        || () &lt; () &amp;&amp; () &gt;= ())
                        &amp;&amp; (() &lt;= () || () &lt;= ())) {
                    if (() + (() - ()) / (() - ())
                            * (() - ()) &lt; ()) {
                        isInArea = !isInArea;
                    }
                }

                j = i;
            }

            if (isInArea)  return true; //Inside the area        }catch (Exception e){

        }
        return false; //Not inside the area
    }

    public static void main(String[] args) {
        String filePath = "C:\\Users\\Administrator\\Desktop\\Forest Chief\\";
        (getGenjsonPosition(filePath,39.34727,108.94647));
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.