source: osm/applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/AddressNode.java@ 24014

Last change on this file since 24014 was 24014, checked in by oliverw, 14 years ago

Renamed getStreet -> getStreetName.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Id Date Revision
  • Property svn:mime-type set to text/plain
File size: 8.4 KB
Line 
1/*
2 * This program is free software: you can redistribute it and/or modify it under
3 * the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 * See the GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License along with this program.
12 * If not, see <http://www.gnu.org/licenses/>.
13 */
14package org.openstreetmap.josm.plugins.fixAddresses;
15
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17
18public class AddressNode extends NodeEntityBase {
19 public static final String MISSING_TAG = "?";
20
21 private String guessedStreetName;
22
23 public AddressNode(OsmPrimitive osmObject) {
24 super(osmObject);
25 }
26
27 /* (non-Javadoc)
28 * @see org.openstreetmap.josm.plugins.addressEdit.NodeEntityBase#setOsmObject(org.openstreetmap.josm.data.osm.OsmPrimitive)
29 */
30 @Override
31 public void setOsmObject(OsmPrimitive osmObject) {
32 super.setOsmObject(osmObject);
33 }
34
35 /**
36 * Checks if the underlying address node has all tags usually needed to describe an address.
37 * @return
38 */
39 public boolean isComplete() {
40 return TagUtils.hasAddrCityTag(osmObject) && TagUtils.hasAddrCountryTag(osmObject) &&
41 TagUtils.hasAddrHousenumberTag(osmObject) && TagUtils.hasAddrPostcodeTag(osmObject) &&
42 TagUtils.hasAddrStateTag(osmObject) && TagUtils.hasAddrStreetTag(osmObject);
43 }
44
45 /**
46 * Gets the name of the street associated with this address.
47 * @return
48 */
49 public String getStreetName() {
50 if (osmObject == null) return MISSING_TAG;
51 /*
52 if (!TagUtils.hasAddrStreetTag(osmObject)) {
53 // check, if referrers have a street
54 for (OsmPrimitive osm : osmObject.getReferrers()) {
55 if (TagUtils.hasAddrStreetTag(osm)) {
56 String refStreetName = TagUtils.getAddrStreetValue(osm);
57
58 if (!StringUtils.isNullOrEmpty(refStreetName)) {
59 return refStreetName;
60 }
61 }
62 }
63 return MISSING_TAG; // nothing found
64 }*/
65 if (!TagUtils.hasAddrStreetTag(osmObject)) {
66 return MISSING_TAG;
67 } else {
68 String sName = TagUtils.getAddrStreetValue(osmObject);
69 if (!StringUtils.isNullOrEmpty(sName)) {
70 return sName;
71 } else {
72 return MISSING_TAG;
73 }
74 }
75 }
76
77 /**
78 * Returns <tt>true</tt>, if this address node has a street name.
79 * @return
80 */
81 public boolean hasStreetName() {
82 return TagUtils.hasAddrStreetTag(osmObject);
83 }
84
85 /**
86 * Returns the street name guessed by the nearest-neighbour search.
87 * @return the guessedStreetName
88 */
89 public String getGuessedStreetName() {
90 return guessedStreetName;
91 }
92
93 /**
94 * @param guessedStreetName the guessedStreetName to set
95 */
96 public void setGuessedStreetName(String guessedStreetName) {
97 this.guessedStreetName = guessedStreetName;
98 //fireEntityChanged(this);
99 }
100
101 public boolean hasGuessedStreetName() {
102 return !StringUtils.isNullOrEmpty(guessedStreetName);
103 }
104
105 /**
106 * Returns true, if this instance has guesses regarding address tags.
107 * @return
108 */
109 public boolean hasGuesses() {
110 return hasGuessedStreetName(); // to be extended later
111 }
112
113 /**
114 * Applies all guessed tags for this node.
115 */
116 public void applyAllGuesses() {
117 if (hasGuessedStreetName()) applyGuessedStreet();
118 }
119
120 /**
121 * Gets the name of the post code associated with this address.
122 * @return
123 */
124 public String getPostCode() {
125 if (!TagUtils.hasAddrPostcodeTag(osmObject)) {
126 return MISSING_TAG;
127 }
128 return TagUtils.getAddrPostcodeValue(osmObject);
129 }
130
131 /**
132 * Gets the name of the house number associated with this address.
133 * @return
134 */
135 public String getHouseNumber() {
136 if (!TagUtils.hasAddrHousenumberTag(osmObject)) {
137 return MISSING_TAG;
138 }
139 return TagUtils.getAddrHousenumberValue(osmObject);
140 }
141
142 /**
143 * Gets the name of the city associated with this address.
144 * @return
145 */
146 public String getCity() {
147 if (!TagUtils.hasAddrCityTag(osmObject)) {
148 return MISSING_TAG;
149 }
150 return TagUtils.getAddrCityValue(osmObject);
151 }
152
153 /**
154 * Gets the name of the state associated with this address.
155 * @return
156 */
157 public String getState() {
158 if (!TagUtils.hasAddrStateTag(osmObject)) {
159 return MISSING_TAG;
160 }
161 return TagUtils.getAddrStateValue(osmObject);
162 }
163
164 /**
165 * Gets the name of the state associated with this address.
166 * @return
167 */
168 public String getCountry() {
169 if (!TagUtils.hasAddrCountryTag(osmObject)) {
170 return MISSING_TAG;
171 }
172 return TagUtils.getAddrCountryValue(osmObject);
173 }
174
175 /**
176 * Removes all addresss related tags from the node or way.
177 */
178 public void removeAllAddressTags() {
179 removeOSMTag(TagUtils.ADDR_CITY_TAG);
180 removeOSMTag(TagUtils.ADDR_COUNTRY_TAG);
181 removeOSMTag(TagUtils.ADDR_POSTCODE_TAG);
182 removeOSMTag(TagUtils.ADDR_HOUSENUMBER_TAG);
183 removeOSMTag(TagUtils.ADDR_STATE_TAG);
184 removeOSMTag(TagUtils.ADDR_STREET_TAG);
185 }
186
187 /* (non-Javadoc)
188 * @see org.openstreetmap.josm.plugins.addressEdit.NodeEntityBase#compareTo(org.openstreetmap.josm.plugins.addressEdit.INodeEntity)
189 */
190 @Override
191 public int compareTo(INodeEntity o) {
192 if (o == null || !(o instanceof AddressNode)) {
193 return -1;
194 }
195 AddressNode other = (AddressNode) o;
196
197 int cc = 0;
198 cc = this.getCountry().compareTo(other.getCountry());
199 if ( cc == 0) {
200 cc = this.getState().compareTo(other.getState());
201 if (cc == 0) {
202 cc = this.getCity().compareTo(other.getCity());
203 if (cc == 0) {
204 cc = this.getStreetName().compareTo(other.getStreetName());
205 if (cc == 0) {
206 if (hasGuessedStreetName()) {
207 if (other.hasStreetName()) {
208 // Compare guessed name with the real name
209 cc = this.getGuessedStreetName().compareTo(other.getStreetName());
210 if (cc == 0) {
211 cc = this.getHouseNumber().compareTo(other.getHouseNumber());
212 }
213 } else if (other.hasGuessedStreetName()){
214 // Compare guessed name with the guessed name
215 cc = this.getGuessedStreetName().compareTo(other.getGuessedStreetName());
216 if (cc == 0) {
217 cc = this.getHouseNumber().compareTo(other.getHouseNumber());
218 }
219 } // else: give up
220 // No guessed name at all -> just compare the number
221 } else {
222 cc = this.getHouseNumber().compareTo(other.getHouseNumber());
223 }
224 }
225 }
226 }
227 }
228
229 return cc;
230 }
231
232 /**
233 * Applies the street name from the specified street node.
234 * @param node
235 */
236 public void assignStreet(StreetNode node) {
237 if (node == null || !node.hasName()) return;
238
239 if (!node.getName().equals(getStreetName())) {
240 setStreetName(node.getName());
241 node.addAddress(this);
242 fireEntityChanged(this);
243 }
244 }
245
246 /**
247 * Applies the guessed street name to the addr:street tag value.
248 */
249 public void applyGuessedStreet() {
250 if (hasGuessedStreetName()) {
251 setOSMTag(TagUtils.ADDR_STREET_TAG, guessedStreetName);
252 guessedStreetName = null;
253 }
254 }
255
256 /**
257 * Sets the street name of the address node.
258 * @param streetName
259 */
260 public void setStreetName(String streetName) {
261 if (streetName != null && streetName.length() == 0) return;
262
263 setOSMTag(TagUtils.ADDR_STREET_TAG, streetName);
264 }
265
266
267 /**
268 * Sets the state of the address node.
269 * @param state
270 */
271 public void setState(String state) {
272 if (state != null && state.length() == 0) return;
273
274 setOSMTag(TagUtils.ADDR_STATE_TAG, state);
275 }
276
277 /**
278 * Sets the country of the address node.
279 * @param country
280 */
281 public void setCountry(String country) {
282 if (country != null && country.length() == 0) return;
283
284 setOSMTag(TagUtils.ADDR_COUNTRY_TAG, country);
285 }
286
287 /**
288 * Sets the post code of the address node.
289 * @param postCode
290 */
291 public void setPostCode(String postCode) {
292 if (postCode != null && postCode.length() == 0) return;
293
294 setOSMTag(TagUtils.ADDR_POSTCODE_TAG, postCode);
295 }
296
297 @Override
298 public String toString() {
299 return AddressNode.getFormatString(this);
300 }
301
302 public static String getFormatString(AddressNode node) {
303 // TODO: Add further countries here
304 // DE
305 String guessed = node.getGuessedStreetName();
306 String sName = node.getStreetName();
307 if (!StringUtils.isNullOrEmpty(guessed) && MISSING_TAG.equals(sName)) {
308 sName = String.format("(%s)", guessed);
309 }
310
311 return String.format("%s %s, %s-%s %s (%s) ",
312 sName,
313 node.getHouseNumber(),
314 node.getCountry(),
315 node.getPostCode(),
316 node.getCity(),
317 node.getState());
318 }
319}
Note: See TracBrowser for help on using the repository browser.