Skip to content

Commit

Permalink
Restrict search authority (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmpoppe authored Jul 12, 2020
1 parent 5f61d1a commit b9ed9ca
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 45 deletions.
4 changes: 2 additions & 2 deletions fhrsPlugin/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.7
Created-By: 1.8.0_251-b08 (Oracle Corporation)
Plugin-Mainversion: 16538
Plugin-Version: 0.1.5
Plugin-Version: 0.2
Plugin-Class: org.openstreetmap.josm.plugins.fhrs.FHRSPlugin
Plugin-Description: Search establishments in FHRS API and merge data i
nto OSM
Plugin-Date: 2020-06-28T11:16:43.534
Plugin-Date: 2020-07-12T06:22:15.920
Author: Kai Michael Poppe
Plugin-Link: https://wiki.openstreetmap.org/wiki/User:Kmpoppe/Plugins#
fhrsPlugin
Expand Down
2 changes: 1 addition & 1 deletion fhrsPlugin/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project name="fhrsPlugin" default="dist" basedir=".">
<property name="commit.message" value="FHRS"/>
<property name="plugin.main.version" value="16538"/>
<property name="version.entry.commit.revision" value="0.1.5"/>
<property name="version.entry.commit.revision" value="0.2"/>
<property name="plugin.author" value="Kai Michael Poppe"/>
<property name="plugin.class" value="org.openstreetmap.josm.plugins.fhrs.FHRSPlugin"/>
<property name="plugin.description" value="Search establishments in FHRS API and merge data into OSM"/>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions fhrsPlugin/src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"d:\\_dev\\osm\\josm\\josm\\core\\dist\\josm-custom.jar",
"d:\\_dev\\osm\\josm\\josm\\plugins\\fhrs\\lib\\gson-2.8.6.jar"
]
}
190 changes: 150 additions & 40 deletions fhrsPlugin/src/org/openstreetmap/josm/plugins/fhrs/FHRSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.w3c.dom.Document;

import java.awt.*;
import javax.swing.*;

Expand All @@ -45,7 +47,7 @@ public class FHRSPlugin extends Plugin {
static JMenuItem FHRSSearch;
private OsmPrimitive selectedObject;

public static List<String> fhrsAuthorities = new ArrayList<String>();
public static List<fhrsAuthority> fhrsAuthorities = new ArrayList<fhrsAuthority>();

FHRSPlugin me = this;
int maxMenuItemLen = 50;
Expand Down Expand Up @@ -81,7 +83,12 @@ public FHRSPlugin(final PluginInformation info) {
.getAsJsonArray("authorities");
for(JsonElement jAuthorityElement: jAuthoritiesArray) {
JsonObject jAuthorityObject = jAuthorityElement.getAsJsonObject();
fhrsAuthorities.add(jAuthorityObject.get("Name").toString().replaceAll("\"([^\"]*)\"", "$1"));
fhrsAuthorities.add(
new fhrsAuthority(
jAuthorityObject.get("Name").toString().replaceAll("\"([^\"]*)\"", "$1"),
Integer.parseInt(jAuthorityObject.get("LocalAuthorityId").toString())
)
);
}
} catch (Exception e) {
displayStackTrace(e);
Expand Down Expand Up @@ -133,6 +140,8 @@ public void actionPerformed(ActionEvent event) {
@Override
public void actionPerformed(ActionEvent event) {
DataSet currentDataSet = MainApplication.getLayerManager().getActiveDataSet();
boolean cancelAction = false;
List<Integer> fhrsAuthorities = new ArrayList<Integer>();
if (currentDataSet != null && currentDataSet.getAllSelected().size() > 0) {
if (currentDataSet.getAllSelected().size() > 1) {
msgBox("More than one object selected", JOptionPane.WARNING_MESSAGE);
Expand All @@ -152,19 +161,35 @@ public void actionPerformed(ActionEvent event) {
}
if (!moreThanHousenumber) {
JPanel inputPanel = new JPanel(new BorderLayout());
JTextField textField = new JTextField(addressInput);
JCheckBox remember = new JCheckBox("Remember value", true);
inputPanel.add(new JLabel("The object doesn't have any address information. Please enter at least a city and/or a full address."), "First");
inputPanel.add(textField, "Center");
inputPanel.add(remember, "Last");
JOptionPane.showOptionDialog(null, inputPanel, "FHRS Plugin", JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
if (textField.getText() != null && textField.getText().trim() != "") {
if (remember.isSelected()) addressInput = textField.getText().trim();
thisAddress = textField.getText().trim();
moreThanHousenumber = true;
inputPanel.add(new JLabel("The object doesn't have any address information. Please choose an action."), "First");
Object[] options1 = { "Enter address", "Search surrounding FHRS authorities (takes time)", "Cancel"};
int result = JOptionPane.showOptionDialog(null, inputPanel, "FHRS Plugin", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, null);
if (result == JOptionPane.CANCEL_OPTION) {
cancelAction = true;
} else {
if (result == JOptionPane.YES_OPTION) {
inputPanel = new JPanel(new BorderLayout());
JTextField textField = new JTextField(addressInput);
JCheckBox remember = new JCheckBox("Remember value", true);
inputPanel.add(new JLabel("The object doesn't have any address information. Please enter at least a city and/or a full address."), "First");
inputPanel.add(textField, "Center");
inputPanel.add(remember, "Last");
JOptionPane.showOptionDialog(null, inputPanel, "FHRS Plugin", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
if (textField.getText() != null && textField.getText().trim() != "") {
if (remember.isSelected()) addressInput = textField.getText().trim();
thisAddress = textField.getText().trim();
moreThanHousenumber = true;
}
}
if (result == JOptionPane.NO_OPTION) {
fhrsAuthorities = getSurroundingAuthorities(selectedObject);
if (fhrsAuthorities.size() > 0) {
moreThanHousenumber = true;
}
}
}
}
if (moreThanHousenumber) {
if (moreThanHousenumber && !cancelAction) {
String returnedJson = "{}";
String cEncodedName = "";
String cEncodedAddress = "";
Expand All @@ -184,37 +209,47 @@ public void actionPerformed(ActionEvent event) {
} )
{
cUrl = cApiEst + "?pagesize=30";
if (fhrsAuthorities.size() > 0)
cUrl = cUrl + "&localAuthorityId=" + fhrsAuthorities.get(0).toString();
for(String par: pars) cUrl = cUrl + par;
returnedJson = fhrsApiCall(cUrl);
if (gson.fromJson(returnedJson, JsonObject.class).getAsJsonArray("establishments").size() > 0) break;
}
jEstablishmentsArray =
gson
.fromJson(returnedJson, JsonObject.class)
.getAsJsonArray("establishments");
List<List<String>> searchResults = new ArrayList<List<String>>();
for(JsonElement jEstablishmentElement: jEstablishmentsArray) {
JsonObject jEstablishmentObject = jEstablishmentElement.getAsJsonObject();
List<String> tableEntry = new ArrayList<String>();
String ApiFHRSID = jEstablishmentObject.get("FHRSID").toString();
String ApiBusinessName = jEstablishmentObject.get("BusinessName").toString().replaceAll("\"([^\"]*)\"", "$1");
tableEntry.add(ApiFHRSID);
tableEntry.add(ApiBusinessName);
String ApiFullAddress = "";
for (int iAddrLine = 1; iAddrLine < 5; iAddrLine++) {
String ApiAddressEntry = jEstablishmentObject
.get("AddressLine" + Integer.toString(iAddrLine))
.toString()
.replaceAll("\"([^\"]*)\"", "$1");
ApiFullAddress += " " + ApiAddressEntry.trim();
ApiFullAddress = ApiFullAddress.trim();
JsonObject thisJsonObject = gson.fromJson(returnedJson, JsonObject.class);
if (thisJsonObject.has("establishments")) {
if (thisJsonObject.getAsJsonArray("establishments").size() > 0) break;
}
tableEntry.add(ApiFullAddress);
searchResults.add(tableEntry);
}
String selectedFhrsId = searchResultsDialog.showSearchDialog(searchResults);
if (selectedFhrsId != null) {
updateObjectData(selectedFhrsId);
JsonObject thisJsonObject = gson.fromJson(returnedJson, JsonObject.class);
if (thisJsonObject.has("establishments")) {
jEstablishmentsArray =
gson
.fromJson(returnedJson, JsonObject.class)
.getAsJsonArray("establishments");
List<List<String>> searchResults = new ArrayList<List<String>>();
for(JsonElement jEstablishmentElement: jEstablishmentsArray) {
JsonObject jEstablishmentObject = jEstablishmentElement.getAsJsonObject();
List<String> tableEntry = new ArrayList<String>();
String ApiFHRSID = jEstablishmentObject.get("FHRSID").toString();
String ApiBusinessName = jEstablishmentObject.get("BusinessName").toString().replaceAll("\"([^\"]*)\"", "$1");
tableEntry.add(ApiFHRSID);
tableEntry.add(ApiBusinessName);
String ApiFullAddress = "";
for (int iAddrLine = 1; iAddrLine < 5; iAddrLine++) {
String ApiAddressEntry = jEstablishmentObject
.get("AddressLine" + Integer.toString(iAddrLine))
.toString()
.replaceAll("\"([^\"]*)\"", "$1");
ApiFullAddress += " " + ApiAddressEntry.trim();
ApiFullAddress = ApiFullAddress.trim();
}
tableEntry.add(ApiFullAddress);
searchResults.add(tableEntry);
}
String selectedFhrsId = searchResultsDialog.showSearchDialog(searchResults);
if (selectedFhrsId != null) {
updateObjectData(selectedFhrsId);
}
} else {
msgBox("No entry found in database", JOptionPane.INFORMATION_MESSAGE);
}
} catch (FileNotFoundException e) {
msgBox("FHRS ID " + selectedObject.get("fhrs:id").toString() + " not found in database.", JOptionPane.ERROR_MESSAGE);
Expand Down Expand Up @@ -434,4 +469,79 @@ public oldAndNewValues(String newV, String oldV) {
this.oldValue = oldV;
}
}
public static class fhrsAuthority {
public String name;
public Integer id;
public fhrsAuthority(String name, Integer id) {
this.name = name;
this.id = id;
}
}
private List<Integer> getSurroundingAuthorities (OsmPrimitive osm) {
List<Integer> returnValue = new ArrayList<Integer>();
try {
String centerSelector = "osm" + (
osm.getType() == OsmPrimitiveType.NODE ? "node" : (
osm.getType() == OsmPrimitiveType.WAY ? "way" : (
osm.getType() == OsmPrimitiveType.RELATION ? "rel" : "")))
+ ":" + Long.toString(osm.getId());
String querySelect = "select ?fhrsAuth (min(?distance) as ?md) with {\n" +
" select ?center where {\n" +
" " + centerSelector + " osmm:loc ?center.\n" +
" }\n" +
"} as %center where { \n" +
" ?element osmt:fhrs:authority ?fhrsAuth;\n" +
" osmm:loc ?coords.\n" +
"\n" +
" include %center.\n" +
" service wikibase:around {\n" +
" ?element osmm:loc ?location.\n" +
" bd:serviceParam wikibase:center ?center;\n" +
" wikibase:radius \"10\"; # in km\n" +
" wikibase:distance ?distance.\n" +
" }\n" +
"}\n" +
"group by ?fhrsAuth\n" +
"order by asc(?md)\n" +
"limit 3";

URL url = new URL("https://sophox.org/sparql?query=" + URLEncoder.encode(querySelect, StandardCharsets.UTF_8.toString()));

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int status = con.getResponseCode();

Reader streamReader = null;

if (status > 299) {
streamReader = new InputStreamReader(con.getErrorStream(), Charset.defaultCharset());
} else {
streamReader = new InputStreamReader(con.getInputStream(), Charset.defaultCharset());
}

BufferedReader in = new BufferedReader(streamReader);
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}

Document doc = XmlHelper.convertStringToXMLDocument(content.toString());
List<String> authorities = XmlHelper.evaluateXPath(doc, "/sparql/results/result/binding[@name='fhrsAuth']/literal/text()");

for(String a: authorities) {
if (fhrsAuthorities.stream().filter(o -> o.name.equals(a)).findFirst().isPresent()) {
returnValue.add(fhrsAuthorities.stream().filter(o -> o.name.equals(a)).findFirst().get().id);
}
}

in.close();
con.disconnect();

} catch (Exception e) {
displayStackTrace(e);
return null;
}
return returnValue;
}
}
55 changes: 55 additions & 0 deletions fhrsPlugin/src/org/openstreetmap/josm/plugins/fhrs/XmlHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.openstreetmap.josm.plugins.fhrs;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XmlHelper {
public static List<String> evaluateXPath(Document document, String xpathExpression) throws Exception
{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
List<String> values = new ArrayList<>();
try
{
XPathExpression expr = xpath.compile(xpathExpression);
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
values.add(nodes.item(i).getNodeValue());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return values;
}

public static Document convertStringToXMLDocument(String xmlString)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;

import org.openstreetmap.josm.plugins.fhrs.FHRSPlugin.fhrsAuthority;

public class mergeTagsDialog {

public static Map<String, String> showTagsDialog (final Map<String, FHRSPlugin.oldAndNewValues> osmTags, boolean warnCapitalization) {
Expand Down Expand Up @@ -82,8 +84,8 @@ public Object getCellEditorValue() {
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value instanceof String && table.getValueAt(row, 1) == "fhrs:authority") {
JComboBox<String> oComboBox = new JComboBox<String>();
for (String auth: FHRSPlugin.fhrsAuthorities) {
oComboBox.addItem(auth);
for (fhrsAuthority auth: FHRSPlugin.fhrsAuthorities) {
oComboBox.addItem(auth.name);
}
oComboBox.setSelectedItem(value.toString());
editor = new DefaultCellEditor(oComboBox);
Expand Down

0 comments on commit b9ed9ca

Please sign in to comment.