Draw WFS with latlong coordinates to canvas in Java - java

I am very new to Java programming, I would appreciate any kind of help.
So I want to display a set of lat-long coordinates (more than 50 coordinates) in Java-based canvas (e.g JFrame, Processing) from a WFS server. I have managed to parse lat-long value and print it to the console. Now I'm stuck in how to bring the lat-long coordinates to the screen coordinates (I'd like to draw it on 1000x500 size). I've tried to search for the reference but couldn't find the simplest one for a beginner like me. Here is the current part of my code :
String[] splitc = coord.split(",");
String lon = splitc[0];
String lat = splitc[1];
//parse string to float
float loncoord=Float.parseFloat(lon);
float latcoord=Float.parseFloat(lat);
Can I transfer the coordinates from the WFS to screen coordinates using world2screen.translate of Geotools library as in https://docs.geotools.org/latest/userguide/library/referencing/axis.html ?
In processing, there is a map() function (https://processing.org/reference/map_.html) to transfer from a range to another. I've tried it but it didn't work on my IDE.
One super noob question, I'm trying to store the WFS connection in a function so I can call it in another class, should I store it in static void or use "return"?
If someone can provide an example of a similar task, that would be very helpful. Thanks (Sara)

You can use this formula instead:
float x = ((WIDTH/360.0) * (180 + loncoord));
float y = ((HEIGHT/180.0) * (90 - latcoord));
It should work... Note that it returns a float and takes 5 arguments of the form:
map(input, inputMin, inputMax, outputMin, outputMax)
You only want to create the connection once, so you're left with two viable options: defining the connection as a static variable of a static class, or defining the connection as an instance variable of a class following the singleton pattern.
Assuming you chose the former approach, the method that returns the connection variable should therefore be static but not void:
public static connectionType getConnection() {
return connectionObject;
}
... where connectionType is the datatype of the connection.

The easiest way is to create a GeoTools WFSDataStore, this code builds up the getCapabilities string if the user has given just a URL to the service endpoint and handles authentication if needed. The datastore is stored in a field of the class:
public FetchWFS(String url, String user, String passwd) throws IOException, URISyntaxException {
if (!user.isEmpty()) {
auth = true;
}
baseURL = new URL(url);
List<NameValuePair> nvp = URLEncodedUtils.parse(baseURL.toURI(), "UTF-8");
NameValuePair service = new BasicNameValuePair("service", "wfs");
NameValuePair request = new BasicNameValuePair("request", "getCapabilities");
NameValuePair version = new BasicNameValuePair("version", "2.0.0");
HashMap<String, NameValuePair> parts = new HashMap<>();
parts.put(service.getName(), service);
parts.put(request.getName(), request);
parts.put(version.getName(), version);
for (NameValuePair part : nvp) {
if (part.getName().equalsIgnoreCase("SERVICE")) {
// We don't care what they think this should be
} else if (part.getName().equalsIgnoreCase("REQUEST")) {
// This must be getCapabuilities so we ignore them
} else if (part.getName().equalsIgnoreCase("VERSION")) {
System.out.println("Changing version to " + part.getValue());
parts.put(version.getName(), part);
} else {
parts.put(part.getName(), part);
}
}
URIBuilder builder = new URIBuilder();
builder.setScheme(baseURL.getProtocol());
builder.setHost(baseURL.getHost());
builder.setPort(baseURL.getPort());
builder.setPath(baseURL.getPath());
List<NameValuePair> p = new ArrayList<>();
p.addAll(parts.values());
builder.setParameters(p);
// builder.addParameter("viewparams", "q:\"mySolrQuery\"");
URI uri = builder.build();
System.out.println(uri);
baseURL = uri.toURL();
// fetch the DataStore
Map<String, Object> params = new HashMap<>();
params.put(WFSDataStoreFactory.URL.key, baseURL);
// params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "mapserver");
if (auth) {
params.put(WFSDataStoreFactory.USERNAME.key, user);
params.put(WFSDataStoreFactory.PASSWORD.key, passwd);
}
// params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "mapserver");
datastore = DataStoreFinder.getDataStore(params);
}
Once you have a DataStore (of any type) you can get a list of available
featureTypes and then add one (or more) of them to a map:
JMapFrame frame = new JMapFrame();
MapContent map = new MapContent();
String[] names = datastore.getNames();
featureSource = store.getFeatureSource(names[0]); //fetch first featureType
schema = featureSource.getSchema();
Style style = SLD.createSimpleStyle(schema);
this.layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
frame.enableToolBar(true);
frame.setMinimumSize(new Dimension(800, 400));
frame.setVisible(true);
The GeoTools Quickstart Tutorial will help you get started with simple mapping, and the Map Styling tutorial will allow you to generate a prettier map when you want more than a simple black and white default map.

Related

How to automate function according to Array list size

I'm sorry this question header is not 100% correct. Because of that, I'll explain my scenario here.
I created a function to merge 4 data sets into one return format. Because that's the format front-end side needed. So this is working fine now.
public ReturnFormat makeThribleLineChart(List<NameCountModel> totalCount, List<NameCountModel>,p1Count, List<NameCountModel> p2Count, List<NameCountModel> average) {
ReturnFormat returnFormat = new ReturnFormat(null,null);
try {
String[] totalData = new String[totalCount.size()];
String[] p1Data = new String[p1Count.size()];
String[] p2Data = new String[p2Count.size()];
String[] averageData = new String[p2Count.size()];
String[] lableList = new String[totalCount.size()];
for (int x = 0; x < totalCount.size(); x++) {
totalData[x] = totalCount.get(x).getCount();
p1Data[x] = p1Count.get(x).getCount();
p2Data[x] = p2Count.get(x).getCount();
averageData[x] = average.get(x).getCount();
lableList[x] = totalCount.get(x).getName();
}
FormatHelper<String[]> totalFormatHelper= new FormatHelper<String[]>();
totalFormatHelper.setData(totalData);
totalFormatHelper.setType("line");
totalFormatHelper.setLabel("Uudet");
totalFormatHelper.setyAxisID("y-axis-1");
FormatHelper<String[]> p1FormatHelper= new FormatHelper<String[]>();
p1FormatHelper.setData(p1Data);
p1FormatHelper.setType("line");
p1FormatHelper.setLabel("P1 päivystykseen heti");
FormatHelper<String[]> p2FormatHelper= new FormatHelper<String[]>();
p2FormatHelper.setData(p2Data);
p2FormatHelper.setType("line");
p2FormatHelper.setLabel("P2 päivystykseen muttei yöllä");
FormatHelper<String[]> averageFormatHelper= new FormatHelper<String[]>();
averageFormatHelper.setData(averageData);
averageFormatHelper.setType("line");
averageFormatHelper.setLabel("Jonotusaika keskiarvo");
averageFormatHelper.setyAxisID("y-axis-2");
List<FormatHelper<String[]>> formatHelpObj = new ArrayList<FormatHelper<String[]>>();
formatHelpObj.add(totalFormatHelper);
formatHelpObj.add(p1FormatHelper);
formatHelpObj.add(p2FormatHelper);
formatHelpObj.add(averageFormatHelper);
returnFormat.setData(formatHelpObj);
returnFormat.setLabels(lableList);
returnFormat.setMessage(Messages.Success);
returnFormat.setStatus(ReturnFormat.Status.SUCCESS);
} catch (Exception e) {
returnFormat.setData(null);
returnFormat.setMessage(Messages.InternalServerError);
returnFormat.setStatus(ReturnFormat.Status.ERROR);
}
return returnFormat;
}
so, as you can see here, all the formatting is hardcoded. So my question is how to automate this code for list count. Let's assume next time I have to create chart formatting for five datasets. So I have to create another function to it. That's the thing I want to reduce. So I hope you can understand my question.
Thank you.
You're trying to solve the more general problem of composing a result object (in this case ReturnFormat) based on dynamic information. In addition, there's some metadata being setup along with each dataset - the type, label, etc. In the example that you've posted, you've hardcoded the relationship between a dataset and this metadata, but you'd need some way to establish this relationship for data dynamically if you have a variable number of parameters here.
Therefore, you have a couple of options:
Make makeThribleLineChart a varargs method to accept a variable number of parameters representing your data. Now you have the problem of associating metadata with your parameters - best option is probably to wrap the data and metadata together in some new object that is provided as each param of makeThribleLineChart.
So you'll end up with a signature that looks a bit like ReturnFormat makeThribleLineChart(DataMetadataWrapper... allDatasets), where DataMetadataWrapper contains everything required to build one FormatHelper instance.
Use a builder pattern, similar to the collection builders in guava, for example something like so:
class ThribbleLineChartBuilder {
List<FormatHelper<String[]>> formatHelpObj = new ArrayList<>();
ThribbleLineChartBuilder addDataSet(String describeType, String label, String yAxisId, List<NameCountModel> data) {
String[] dataArray = ... ; // build your array of data
FormatHelper<String[]> formatHelper = new FormatHelper<String[]>();
formatHelper.setData(dataArray);
formatHelper.setType(describeType);
... // set any other parameters that the FormatHelper requires here
formatHelpObj.add(formatHelper);
return this;
}
ReturnFormat build() {
ReturnFormat returnFormat = new ReturnFormat(null, null);
returnFormat.setData(this.formatHelpObj);
... // setup any other fields you need in ReturnFormat
return returnFormat;
}
}
// usage:
new ThribbleLineChartBuilder()
.addDataSet("line", "Uudet", "y-axis-1", totalCount)
.addDataSet("line", "P1 päivystykseen heti", null, p1Count)
... // setup your other data sources
.build()

Java - Amazon Product Advertising API trouble with searching a specific item

I'm making an app that requires me to get the prices of some items (lets say computer parts) but sometimes this app will make requests like color: black or screen resolution: 1200x720 etc.
The first problem I faced is that the request screen resolution: 1200x720 actually returns items but it suppose to return nothing cause screen resolution is obviously not a computer part.
Example here
How do I specify in request params not to show similar responses but only the keyword screen resolution: 1200x720? (so it will return null)
The only thing I could find was to point the SearchIndex - Electronics but what it is supposed to be is computer parts (cpu, processors, graphic cards etc).
Here is the code that I tried
public static String mainRequest(String keyword) {
/*
* Set up the signed requests helper.
*/
SignedRequestsHelper helper;
try {
helper = SignedRequestsHelper.getInstance(ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
} catch (Exception e) {
e.printStackTrace();
return "Exception";
}
String requestUrl = null;
Map<String, String> params = new HashMap<String, String>();
params.put("Service", "AWSECommerceService");
params.put("Operation", "ItemSearch");
params.put("AWSAccessKeyId", "keyid");
params.put("AssociateTag", "tag");
params.put("SearchIndex", "Electronics");
params.put("ResponseGroup", "Offers");
params.put("Condition", "New");
params.put("Keywords", keyword);
requestUrl = helper.sign(params);
return fetchTitle(requestUrl);
}

Cloudinary list all images not implemented exception

I m trying to list all images from cloudinary using the below code but I get "Not implemented Exception"
Map config = ObjectUtils.asMap(
"cloud_name", Config.CLOUDINARY_CLOUD_NAME,
"api_key", Config.CLOUDINARY_API_KEY,
"api_secret", Config.CLOUDINARY_API_SECRET);
Cloudinary cloudinary = new Cloudinary(config);
Api api = cloudinary.api();
Map result;
try {
result = api.resources(ObjectUtils.emptyMap());
} catch (Exception e) {
e.printStackTrace();
}
Which seems to be coming from
public ApiResponse callApi(HttpMethod method, Iterable<String> uri, Map<String, ? extends Object> params, Map options) throws Exception {
throw new Exception("not implemented");
}
// function parameter values
this = {ApiStrategy#4487}
api = {Api#4499}
shadow$_klass_ = {Class#4338} "class com.cloudinary.android.ApiStrategy"
shadow$_monitor_ = -2091658589
method = {Api$HttpMethod#4395} "GET"
name = {String#4396} "GET"
ordinal = 0
shadow$_klass_ = {Class#4382} "class com.cloudinary.Api$HttpMethod"
shadow$_monitor_ = -2098674761
uri = {ArrayList#4505} size = 2
0 = {String#4400} "resources"
1 = {String#4374} "image"
params = {HashMap#4516} size = 0
options = {Collections$EmptyMap#4371} size = 0
Since Android is a client-side environment and the Admin-API is designated to serve on the server-side, its methods are not implemented for native Android.
Having said that, you can build the HTTP request on your own, using the ADMIN-API endpoint, and issue it in order to collect the information you require.
For more info regarding end points and constructing of the HTTP request, see Cloudinary's docs: http://cloudinary.com/documentation/admin_api

Iterating all Play Framework routes in Test

Is there any way to Iterate all described services in routes file? URL and HTTP methods are needed.
I need this feature for running some integration test.
I am using Play for Java.
Not easily. I managed to hack my way through it a while ago(no scala know-how). I'll post that code maybe it can be of use.
public static List<String[]> parseRoutes() {
scala.Option<play.core.Router.Routes> option = Play.application().getWrappedApplication().routes();
if (option.isDefined()) {
play.core.Router.Routes routes = option.get();
scala.collection.Seq<scala.Tuple3<String, String, String>> doc = routes.documentation();
scala.collection.Iterator<scala.Tuple3<String, String, String>> it = doc.iterator();
List<String[]> listOfRoutes = new ArrayList<String[]>();
while(it.hasNext()) {
scala.Tuple3<String, String, String> tuple = it.next();
//tuple._1() is the method and tuple._2() the url... tuple._3() is the controller name
String[] route = {tuple._1(), tuple._2()};
listOfRoutes.add(route);
Logger.debug("route -> " + Arrays.toString(route));
}
return listOfRoutes;
}
return null;
}
Don't worry about the .iterator() showing a The method iterator() is ambiguous for the type Seq<Tuple3<String,String,String>>. It compiles just fine in play.

Google Custom search API: how to get search result contents description (e.g snippets) for URL

how can we get the contents of the URL retrieved using google custom search API.
I am new to work with such APIs and in documentation no such sample code is given that can explain it. I am using google-api-services-customsearch-v1-rev36-1.17.0-rc.jar
here is my code.
protected Result[] doSearch() {
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer(){
#Override
public void initialize(HttpRequest request) throws IOException {
}
};
JsonFactory jsonFactory = new JacksonFactory();
Customsearch csearch = new Customsearch( new NetHttpTransport(), jsonFactory, httpRequestInitializer);
Customsearch.Cse.List listReqst = csearch.cse().list(query.getQueryString());
listReqst.setKey(GOOGLE_KEY);
// set the search engine ID got from API console
listReqst.setCx("SEARCH_ENGINE_ID");
// set the query string
listReqst.setQ(query); //query contains search query string
// language chosen is English for search results
listReqst.setLr("lang_en");
// set hit position of first search result
listReqst.setStart((long) firstResult);
// set max number of search results to return
listReqst.setNum((long) maxResults);
Search result = list.execute();
// perform search
}
here after this need to get the snippets and URLs of the corresponding websites. which I have to return in this function. how can we retrieve them.
In the final line of your code it executes the query, returns the results, and parses them into that 'Search' object, described here:
https://developers.google.com/resources/api-libraries/documentation/customsearch/v1/java/latest/com/google/api/services/customsearch/model/Search.html
So, to get the URL and snippet for each result you just do:
List<Result> results = result.getItems();
for (Result r : results) {
String url = r.getLink();
String snippet = r.getSnippet();
}
To return all the Results, as per your function signiture above, you just need to convert the list to an array:
List<Result> results = result.getItems();
return results.toArray( new Result[results.size()] );

Categories