Skip to content

Instantly share code, notes, and snippets.

@TravelTime-Frontend
Last active February 21, 2023 08:52
Show Gist options
  • Save TravelTime-Frontend/7c4c860159831ee88b33cbb956c5b475 to your computer and use it in GitHub Desktop.
Save TravelTime-Frontend/7c4c860159831ee88b33cbb956c5b475 to your computer and use it in GitHub Desktop.
Point in polygon Java
package org.example;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.traveltime.sdk.TravelTimeSDK;
import com.traveltime.sdk.auth.TravelTimeCredentials;
import com.traveltime.sdk.dto.common.Coordinates;
import com.traveltime.sdk.dto.common.Location;
import com.traveltime.sdk.dto.common.Property;
import com.traveltime.sdk.dto.common.transportation.Driving;
import com.traveltime.sdk.dto.requests.TimeFilterRequest;
import com.traveltime.sdk.dto.requests.timefilter.DepartureSearch;
import com.traveltime.sdk.dto.responses.TimeFilterResponse;
import com.traveltime.sdk.dto.responses.errors.TravelTimeError;
import io.vavr.control.Either;
import java.io.Reader;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TimeFilter {
public static void main(String[] args) throws Exception {
var uri = Paths.get("../data/customers.csv").toUri();
var path = Path.of(uri);
List<String[]> data = TimeFilter.readAllLines(path);
List<Location> locations = data.stream().map((line) -> {
var id = Array.get(line, 0).toString();
var lat = Double.parseDouble(Array.get(line, 1).toString());
var lng = Double.parseDouble(Array.get(line, 2).toString());
return new Location(id, new Coordinates(lat, lng));
}).collect(Collectors.toList());
Location departureLocation = locations.get(0);
List<Location> arrivalSearches = locations.subList(1, locations.size() - 1);
var nycOffset = ZoneOffset.of("-05:00");
var departureTime = OffsetDateTime.parse("2022-11-17T15:00:00Z").withOffsetSameLocal(nycOffset).toInstant();
DepartureSearch departureSearch = DepartureSearch
.builder()
.id("departure search example")
.departureLocationId(departureLocation.getId())
.arrivalLocationIds(arrivalSearches.stream().map(Location::getId).collect(Collectors.toList()))
.transportation(new Driving(true))
.travelTime(1800)
.departureTime(departureTime)
.properties(Arrays.asList(Property.TRAVEL_TIME, Property.DISTANCE))
.build();
TimeFilterRequest request = TimeFilterRequest
.builder()
.locations(locations)
.departureSearches(Collections.singletonList(departureSearch))
.build();
TravelTimeCredentials credentials = new TravelTimeCredentials("YOUR_APP_ID", "YOUR_APP_KEY");
TravelTimeSDK sdk = new TravelTimeSDK(credentials);
Either<TravelTimeError, TimeFilterResponse> response = sdk.send(request);
if (response.isRight()) {
System.out.println(
response.get().getResults().get(0).getUnreachable().size()
+ " customers out of 500 are unreachable withing 30 minute driving time ");
} else {
System.out.println(response.getLeft().toString());
}
}
public static List<String[]> readAllLines(Path filePath) throws Exception {
try (Reader reader = Files.newBufferedReader(filePath)) {
try (CSVReader csvReader = new CSVReaderBuilder(reader).withSkipLines(1).build()) {
return csvReader.readAll();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment