Sprawdzanie gmin w tracku
Potrzebny jest kontroler z metodą, która przyjmuje uploadowany plik. Następnie plik zapisujemy w jakimś tempie z unikalną nazwą, dokonujemy konwersji do JSON, a ten deserializujemy do GeoJSON. Niestety, klasy Springowe niezbyt się nadają, bo nie mają setterów, które chce ObjectMapper z Jackson. Rozwiązaniem jest napisanie własnych, które implementują ten interfejs.
@Data
@NoArgsConstructor
public class GeoJsonPoint implements GeoJson<Iterable<Double>> {
private final String type = "Point";
PointTuple coordinates;
public GeoJsonPoint(double x, double y) {
coordinates=new PointTuple(x,y);
}
public void setCoordinates(List<Double> list){
coordinates= new PointTuple(list.get(0),list.get(1));
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PointTuple implements Iterable<Double>{
Double x;
Double y;
@Override
public Iterator<Double> iterator() {
return new Iterator<Double>() {
int index=0;
@Override
public boolean hasNext() {
if(index<2)
return true;
return false;
}
@Override
public Double next() {
index++;
if(index==1)
return x;
else if (index==2)
return y;
return null;
}
};
}
}
}
@Data
@NoArgsConstructor
public class GeoJsonMultiLineString implements GeoJson<Iterable<GeoJsonLineString>>{
private final String type = "MultiLineString";
List<GeoJsonLineString> coordinates = new ArrayList<>();
public GeoJsonMultiLineString(List<GeoJsonLineString> coordinates) {
this.coordinates=coordinates;
}
public void setCoordinates(List<List<List<Double>>> value){
coordinates = new ArrayList<GeoJsonLineString>();
for (List<List<Double>> it:value) {
GeoJsonLineString lineString = new GeoJsonLineString();
lineString.setCoordinates(it);
coordinates.add(lineString);
}
}
}
GeoJSON MultiLineString składa się z listy LineString, a ten składa się z listy Point. W ten sposób możemy zaimplementować wszystkie klasy GeoJSON. Należy też przerobić konwertery z org.springframework.data.mongodb.core.convert.GeoConverters. Dzięki temu można bezpośrednio używać własnych klas do komunikacji z MongoDB. Rejestrujemy je poprzez rozszerzenie klasy CustomConverters i przekazanie jej listy konwerterów.
Jeśli mamy już track w postaci GeoJSON, to wystarczy zawołać zrobioną wcześniej metodę z GminaRepository. Zwróci ona listę gmin, którą odsyłamy w zwrotce.