同步地图和.xml文件中的坐标 [英] Sync coordinates from map and .xml file

查看:106
本文介绍了同步地图和.xml文件中的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
很高兴看到这么多人互相帮助.这次我被困住了,我需要帮助.或者更精确地寻找更快的解决方案.
我不是要重塑GPS软件或地图系统,而是需要在vb.net中创建类似的软件.它也可以在触摸屏上运行.
该地图大于屏幕尺寸,并且显然可以滚动(7000px x 2000px).
当我单击它时,我会在地图上获得该点的MapX和MapY坐标.我需要从.xml文件中获取对应的信息.有600个POI,每个POI是一个正方形(50像素x 50像素).我的算法遵循以下步骤:
-使用coordX<获取每个.xml节点(POI). MapX并添加到临时列表
-在此列表中,仅保留coordX + 50> MapX
-新的淘汰标准:CoordY< MapY
-最后标准:CoordY + 50> MapY
搜索结束时,只有一个节点,我可以从中读取值:ID,有关地点的信息以及其他相关信息
.xml的结构:(所有位置和坐标都是虚构的)
< POIs>
< POI>
< CoordX> 11</CoordX>
< CoordY> 12</CoordY>
< ID> 1</ID>
< Info>博物馆</Info>
<描述>国家博物馆....</描述>
</POI>
< POI>
< CoordX> 22</CoordX>
< CoordY> 23</CoordY>
< ID> 2</ID>
< Info>警察局</Info>
<说明>第十三区</说明>
</POI>
.....
</POIs>

这是找到POI的最快方法吗?我可以结合所有4条条件仅在一次搜索中找到节点吗?
如果答案是肯定的,您能给我一个示例代码吗?还是将我指向一个现有线程?

Hello people.
It''s nice to see so many people helping each other. This time I''m stuck and I need help. Or to be more precisely in search for a faster solution.
I''m not trying to reinvent GPS software or mapping system but I need to create a similar software in vb.net. It will work on a touch screen too.
The map is larger than screen size and obviously scrollable (7000px x 2000px).
When I click on it I get the MapX and MapY coordinates of that point on the map. I need to get from an .xml file the correspondent info. There are 600 POIs and every POI is a square (50px x 50px). My algorithm follows these steps:
- get every .xml node (POI) with coordX < MapX and add to a temporary list
- in this list keep only nodes with coordX + 50 > MapX
- new elimination criteria: CoordY < MapY
- last criteria: CoordY + 50 > MapY
At the end of the search there is only one node and I can read values from it: ID, information about place, other related info
Structure of .xml: (all places and coordinates are fictive)
<POIs>
<POI>
<CoordX>11</CoordX>
<CoordY>12</CoordY>
<ID>1</ID>
<Info>Museum</Info>
<Description>National Museum of ....</Description>
</POI>
<POI>
<CoordX>22</CoordX>
<CoordY>23</CoordY>
<ID>2</ID>
<Info>Police Station</Info>
<Description>13th Precinct</Description>
</POI>
.....
</POIs>

Is this the fastest method to find the POI ? Can I combine all 4 criterias to find in only one search the node ?
If the answer is yes can you give me an example of code ? Or point me to an existing thread ?

推荐答案



您的算法使用已修改的临时列表.那不是很有效,直接选择最好不要创建任何临时列表会更好(请参见下面的示例).

如果您有600个POI,那真的很麻烦.鉴于您不从磁盘传送时间中读取XML(这很昂贵!)并且不创建中间列表,因此它应该很快.

但是使解决方案更快的一种方法是将所有POI分组为一个二维数组,将POI集中在位置上,因此仅需搜索其中的一个子集.

这是一个快速而肮脏的实现(未经测试,不幸的是在C#中-但我不做VB.NET:cool :).它应该给您提示如何更快地实现:

Hi,

Your algorithm uses a temporary list which is modified. That is not very effective, it is much better to do the selection directly and never create any temporary list (see example attached below for an example).

If you have 600 POI''s it is really noy that much. Given that you do not read the XML from disk eveytime (that is expensive!) and do not create intermediate lists it should be fast.

But one way of getting the solution to be faster is to group all POIs in a 2-dimensional array gruping the POIs on location so only a subset of them has to be searched.

Here is a quick and dirty implementation (not tested, and unfortunately in C# - but I don''t do VB.NET :cool:). It should give you a hint of how to get faster implementation:

public class POI {
    public int X { get; set; }
    public int Y { get; set; }
    public string Value { get; set; }
}
public class PoiStore {
    public const int MAP_SIZE_X = 7000;
    public const int MAP_SIZE_Y = 2000;
    public const int DELTA = 100;
    private readonly IList<POI>[,] _array = new IList<POI>[MAP_SIZE_X/DELTA, MAP_SIZE_Y/DELTA];
    /// <summary>
    /// Store in an array that collects all POIs in a DELTA sized grid.
    /// </summary>
    private void Store(POI poi) {
        var x = poi.X / DELTA;
        var y = poi.Y / DELTA;
        var list = _array[x, y];
        if (list == null) {
            list = new List<POI>();
            _array[x, y] = list;
        }
        list.Add(poi);
    }
    /// <summary>
    /// Read the XML and store in an array
    /// </summary>
    public void ReadXml(string url) {
        var doc = XDocument.Load(url);
        foreach (var uriElement in doc.Root.Elements("URI")) {
            Store(new POI {
                X = int.Parse(uriElement.Element("CoordX").Value),
                Y = int.Parse(uriElement.Element("CoordY").Value),
                Value = uriElement.Element("Value").Value
            });
        }
    }
    /// <summary>
    /// Find all POIs that are within the square given by <c>from</c> and <c>to</c>
    /// </summary>
    public IEnumerable<POI> Find(Point from, Point to) {
        for (var xIndex = from.X / DELTA; xIndex <= to.X / DELTA; xIndex++) {
            for (var yIndex = from.Y / DELTA; yIndex <= to.Y / DELTA; yIndex++) {
                var list = _array[xIndex,yIndex];
                if (list == null) continue;
                foreach (var poi in list) {
                    if (from.X <= poi.X && poi.X <= to.X && from.Y <= poi.Y && poi.Y <= to.Y) {
                        yield return poi;
                    }
                }
            }
        }
    }
}


这篇关于同步地图和.xml文件中的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆