2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 * @brief Contains code that makes navit able to load maps
24 * This file contains the code that makes navit able to load maps. Because
25 * navit is able to open maps in different formats, this code does not handle
26 * any map format itself. This is done by map plugins which register to this
27 * code by calling plugin_register_map_type().
29 * When opening a new map, the map plugin will return a pointer to a map_priv
30 * struct, which can be defined by the map plugin and contains whatever private
31 * data the map plugin needs to access the map. This pointer will also be used
32 * as a "handle" to access the map opened.
34 * A common task is to create a "map rect". A map rect is a rectangular part of
35 * the map, that one can for example retrieve items from. It is not possible to
36 * retrieve items directly from the complete map. Creating a map rect returns a
37 * pointer to a map_rect_priv, which contains private data for the map rect and
38 * will be used as "handle" for this map rect.
45 #include "projection.h"
49 #include "transform.h"
55 * @brief Holds information about a map
57 * This structure holds information about a map.
60 struct map_methods meth; /**< Structure with pointers to the map plugin's functions */
61 struct map_priv *priv; /**< Private data of the map, only known to the map plugin */
62 struct attr **attrs; /**< Attributes of this map */
63 struct callback_list *attr_cbl; /**< List of callbacks that are called when attributes change */
67 * @brief Describes a rectangular extract of a map
69 * This structure describes a rectangular extract of a map.
72 struct map *m; /**< The map this extract is from */
73 struct map_rect_priv *priv; /**< Private data of this map rect, only known to the map plugin */
77 * @brief Opens a new map
79 * This function opens a new map based on the attributes passed. This function
80 * takes the attribute "attr_type" to determine which type of map to open and passes
81 * all attributes to the map plugin's function that was specified in the
82 * plugin_register_new_map_type()-call.
84 * Note that every plugin should accept an attribute of type "attr_data" to be passed
85 * with the filename of the map to be opened as value.
87 * @param attrs Attributes specifying which map to open, see description
88 * @return The opened map or NULL on failure
91 map_new(struct attr *parent, struct attr **attrs)
94 struct map_priv *(*maptype_new)(struct map_methods *meth, struct attr **attrs);
95 struct attr *type=attr_search(attrs, NULL, attr_type);
98 dbg(0,"missing type\n");
101 maptype_new=plugin_get_map_type(type->u.str);
103 dbg(0,"invalid type '%s'\n", type->u.str);
107 m=g_new0(struct map, 1);
108 m->attrs=attr_list_dup(attrs);
109 m->priv=maptype_new(&m->meth, attrs);
115 m->attr_cbl=callback_list_new();
120 * @brief Gets an attribute from a map
122 * @param this_ The map the attribute should be read from
123 * @param type The type of the attribute to be read
124 * @param attr Pointer to an attrib-structure where the attribute should be written to
125 * @param iter (NOT IMPLEMENTED) Used to iterate through all attributes of a type. Set this to NULL to get the first attribute, set this to an attr_iter to get the next attribute
126 * @return True if the attribute type was found, false if not
129 map_get_attr(struct map *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
131 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
135 * @brief Sets an attribute of a map
137 * This sets an attribute of a map, overwriting an attribute of the same type if it
138 * already exists. This function also calls all the callbacks that are registred
139 * to be called when attributes change.
141 * @param this_ The map to set the attribute of
142 * @param attr The attribute to set
143 * @return True if the attr could be set, false otherwise
146 map_set_attr(struct map *this_, struct attr *attr)
148 this_->attrs=attr_generic_set_attr(this_->attrs, attr);
149 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
154 * @brief Registers a new callback for attribute-change
156 * This function registers a new callback function that should be called if the attributes
159 * @param this_ The map to associate the callback with
160 * @param cb The callback to add
163 map_add_callback(struct map *this_, struct callback *cb)
165 callback_list_add(this_->attr_cbl, cb);
169 * @brief Removes a callback from the list of attribute-change callbacks
171 * This function removes one callback from the list of callbacks functions that should be called
172 * when attributes of the map change.
174 * @param this_ The map to remove the callback from
175 * @param cb The callback to remove
178 map_remove_callback(struct map *this_, struct callback *cb)
180 callback_list_remove(this_->attr_cbl, cb);
185 * @brief Checks if strings from a map have to be converted
187 * @param this_ Map to be checked for the need to convert strings
188 * @return True if strings from the map have to be converted, false otherwise
191 map_requires_conversion(struct map *this_)
193 return (this_->meth.charset != NULL && strcmp(this_->meth.charset, "utf-8"));
197 * @brief Converts a string from a map
199 * @param this_ The map the string to be converted is from
200 * @param str The string to be converted
201 * @return The converted string. It has to be map_convert_free()d after use.
204 map_convert_string(struct map *this_, char *str)
206 return g_convert(str, -1,"utf-8",this_->meth.charset,NULL,NULL,NULL);
210 * @brief Frees the memory allocated for a converted string
212 * @param str The string to be freed
215 map_convert_free(char *str)
221 * @brief Returns the projection of a map
223 * @param this_ The map to return the projection of
224 * @return The projection of the map
227 map_projection(struct map *this_)
229 return this_->meth.pro;
233 * @brief Sets the projection of a map
235 * @param this_ The map to set the projection of
236 * @param pro The projection to be set
239 map_set_projection(struct map *this_, enum projection pro)
245 * @brief Destroys an opened map
247 * @param m The map to be destroyed
250 map_destroy(struct map *m)
252 m->meth.map_destroy(m->priv);
253 attr_list_free(m->attrs);
258 * @brief Creates a new map rect
260 * This creates a new map rect, which can be used to retrieve items from a map. If
261 * sel is a linked-list of selections, all of them will be used. If you pass NULL as
262 * sel, this means "get me the whole map".
264 * @param m The map to build the rect on
265 * @param sel Map selection to choose the rectangle - may be NULL, see description
266 * @return A new map rect
269 map_rect_new(struct map *m, struct map_selection *sel)
274 printf("map_rect_new 0x%x,0x%x-0x%x,0x%x\n", r->lu.x, r->lu.y, r->rl.x, r->rl.y);
276 mr=g_new0(struct map_rect, 1);
278 mr->priv=m->meth.map_rect_new(m->priv, sel);
288 * @brief Gets the next item from a map rect
290 * Returns an item from a map rect and advances the "item pointer" one step further,
291 * so that at the next call the next item is returned. Returns NULL if there are no more items.
293 * @param mr The map rect to return an item from
294 * @return An item from the map rect
297 map_rect_get_item(struct map_rect *mr)
300 dbg_assert(mr != NULL);
301 dbg_assert(mr->m != NULL);
302 dbg_assert(mr->m->meth.map_rect_get_item != NULL);
303 ret=mr->m->meth.map_rect_get_item(mr->priv);
310 * @brief Returns the item specified by the ID
312 * @param mr The map rect to search for the item
313 * @param id_hi High part of the ID to be found
314 * @param id_lo Low part of the ID to be found
315 * @return The item with the specified ID or NULL if not found
318 map_rect_get_item_byid(struct map_rect *mr, int id_hi, int id_lo)
320 struct item *ret=NULL;
321 dbg_assert(mr != NULL);
322 dbg_assert(mr->m != NULL);
323 if (mr->m->meth.map_rect_get_item_byid)
324 ret=mr->m->meth.map_rect_get_item_byid(mr->priv, id_hi, id_lo);
331 * @brief Destroys a map rect
333 * @param mr The map rect to be destroyed
336 map_rect_destroy(struct map_rect *mr)
339 mr->m->meth.map_rect_destroy(mr->priv);
345 * @brief Holds information about a search on a map
347 * This structure holds information about a search performed on a map. This can be
348 * used as "handle" to retrieve items from a search.
352 struct attr search_attr;
357 * @brief Starts a search on a map
359 * This function starts a search on a map. What attributes one can search for depends on the
362 * The OSM/binfile plugin currently supports: attr_town_name, attr_street_name
363 * The MG plugin currently supports: ttr_town_postal, attr_town_name, attr_street_name
365 * If you enable partial matches bear in mind that the search matches only the begin of the
366 * strings - a search for a street named "street" would match to "streetfoo", but not to
367 * "somestreet". Search is case insensitive.
369 * The item passed to this function specifies a "superior item" to "search within" - e.g. a town
370 * in which we want to search for a street, or a country in which to search for a town.
372 * Please also note that the search for countries is not handled by map plugins but by navit internally -
373 * have a look into country.c for details. Because of that every map plugin has to accept a country item
374 * to be passed as "superior item".
376 * Note: If you change something here, please make shure to also update the documentation of mapset_search_new()
379 * @param m The map that should be searched
380 * @param item Specifies a superior item to "search within" (see description)
381 * @param search_attr Attribute specifying what to search for. See description.
382 * @param partial Set this to true to also have partial matches. See description.
383 * @return A new map search struct for this search
386 map_search_new(struct map *m, struct item *item, struct attr *search_attr, int partial)
388 struct map_search *this_;
389 dbg(1,"enter(%p,%p,%p,%d)\n", m, item, search_attr, partial);
390 dbg(1,"0x%x 0x%x 0x%x\n", attr_country_all, search_attr->type, attr_country_name);
391 this_=g_new0(struct map_search,1);
393 this_->search_attr=*search_attr;
394 if (search_attr->type >= attr_country_all && search_attr->type <= attr_country_name)
395 this_->priv=country_search_new(&this_->search_attr, partial);
397 if (m->meth.map_search_new) {
399 this_->search_attr.u.str=g_convert(this_->search_attr.u.str, -1,m->meth.charset,"utf-8",NULL,NULL,NULL);
400 this_->priv=m->meth.map_search_new(m->priv, item, &this_->search_attr, partial);
414 * @brief Returns an item from a map search
416 * This returns an item of the result of a search on a map and advances the "item pointer" one step,
417 * so that at the next call the next item will be returned. If there are no more items in the result
420 * @param this_ Map search struct of the search
421 * @return One item of the result
424 map_search_get_item(struct map_search *this_)
430 if (this_->search_attr.type >= attr_country_all && this_->search_attr.type <= attr_country_name)
431 return country_search_get_item(this_->priv);
432 ret=this_->m->meth.map_search_get_item(this_->priv);
439 * @brief Destroys a map search struct
441 * @param this_ The map search struct to be destroyed
444 map_search_destroy(struct map_search *this_)
448 if (this_->search_attr.type >= attr_country_all && this_->search_attr.type <= attr_country_name)
449 country_search_destroy(this_->priv);
451 if (this_->m->meth.charset)
452 g_free(this_->search_attr.u.str);
453 this_->m->meth.map_search_destroy(this_->priv);
459 * @brief Creates a new rectangular map selection
461 * @param center Coordinates of the center of the new rectangle
462 * @param distance Distance of the rectangle's borders from the center
463 * @param order Desired order of the new selection
464 * @return The new map selection
466 struct map_selection *
467 map_selection_rect_new(struct pcoord *center, int distance, int order)
469 struct map_selection *ret=g_new0(struct map_selection, 1);
471 ret->range=item_range_all;
472 ret->u.c_rect.lu.x=center->x-distance;
473 ret->u.c_rect.lu.y=center->y+distance;
474 ret->u.c_rect.rl.x=center->x+distance;
475 ret->u.c_rect.rl.y=center->y-distance;
480 * @brief Duplicates a map selection, transforming coordinates
482 * This duplicates a map selection and at the same time transforms the internal
483 * coordinates of the selection from one projection to another.
485 * @param sel The map selection to be duplicated
486 * @param from The projection used for the selection at the moment
487 * @param to The projection that should be used for the duplicated selection
488 * @return A duplicated, transformed map selection
490 struct map_selection *
491 map_selection_dup_pro(struct map_selection *sel, enum projection from, enum projection to)
493 struct map_selection *next,**last;
494 struct map_selection *ret=NULL;
497 next = g_new(struct map_selection, 1);
499 if (from != projection_none || to != projection_none) {
500 transform_from_to(&sel->u.c_rect.lu, from, &next->u.c_rect.lu, to);
501 transform_from_to(&sel->u.c_rect.rl, from, &next->u.c_rect.rl, to);
511 * @brief Duplicates a map selection
513 * @param sel The map selection to duplicate
514 * @return The duplicated map selection
516 struct map_selection *
517 map_selection_dup(struct map_selection *sel)
519 return map_selection_dup_pro(sel, projection_none, projection_none);
523 * @brief Destroys a map selection
525 * @param sel The map selection to be destroyed
528 map_selection_destroy(struct map_selection *sel)
530 struct map_selection *next;
539 * @brief Checks if a selection contains a rectangle containing an item
541 * This function checks if a selection contains a rectangle which exactly contains
542 * an item. The rectangle is automatically built around the given item.
544 * @param sel The selection to be checked
545 * @param item The item that the rectangle should be built around
546 * @return True if the rectangle is within the selection, false otherwise
549 map_selection_contains_item_rect(struct map_selection *sel, struct item *item)
554 while (item_coord_get(item, &c, 1)) {
559 coord_rect_extend(&r, &c);
564 return map_selection_contains_rect(sel, &r);
570 * @brief Checks if a selection contains a item range
572 * This function checks if a selection contains at least one of the items in range
574 * @param sel The selection to be checked
575 * @param follow Whether the next pointer of the selection should be followed
576 * @param ranges The item ranges to be checked
577 * @count the number of elements in ranges
578 * @return True if there is a match, false otherwise
582 map_selection_contains_item_range(struct map_selection *sel, int follow, struct item_range *range, int count)
588 for (i = 0 ; i < count ; i++) {
589 if (item_range_intersects_range(&sel->range, &range[i]))
599 * @brief Checks if a selection contains a item
601 * This function checks if a selection contains a item type
603 * @param sel The selection to be checked
604 * @param follow Whether the next pointer of the selection should be followed
605 * @param item The item type to be checked
606 * @return True if there is a match, false otherwise
610 map_selection_contains_item(struct map_selection *sel, int follow, enum item_type type)
615 if (item_range_contains_item(&sel->range, type))
627 * @brief Checks if a pointer points to the private data of a map
629 * @param map The map whose private data should be checked.
630 * @param priv The private data that should be checked.
631 * @return True if priv is the private data of map
634 map_priv_is(struct map *map, struct map_priv *priv)
636 return (map->priv == priv);
640 map_dump_filedesc(struct map *map, FILE *out)
642 struct map_rect *mr=map_rect_new(map, NULL);
645 while ((item = map_rect_get_item(mr)))
646 item_dump_filedesc(item, map, out);
647 map_rect_destroy(mr);
651 map_dump_file(struct map *map, const char *file)
655 map_dump_filedesc(map, f);
660 map_dump(struct map *map)
662 map_dump_filedesc(map, stdout);