最近无意翻开4年前做过的一个功能,就是搜集全国各城市各个区(县)的路(XX路、XX道、XX街、XX镇、XX乡、XX屯、XX村、XX社)。众所周知,我们都可以在网上找到省、市、区(县)这三级联动的数据,可是就并没有关于某个城市的某些区(县)下所对应的路(以下所有的路,道,街,镇,乡,屯,村,社统称为路)的数据,不过我们可以找到一些有地址的网站,例如大众点评网,里面就有很多一些饮食店等的具体地址。可以写个爬虫程序,把所有的详细地址先写进数据库的某个表中,然后再执行算法,把地址中有包含路、道、街、镇、乡、屯、村、社等属于第四级的数据写进对应的数据表(当然必须把第四级的数据对应到第三级区(县))。

1、首先需要建五张表(一个用来存放地址的表Address,一个用来存放全国的所有的省Province,一个用来存放属于省的城市City,一个用来存放属于城市的区(县)District,一个用来存放属于区的路Road)。下面是数据表关系图:

所有的表中的FID是外键表的ID,其中省Province的FID暂时没用到可忽略,其中flag是一个标志,如0表示新加,1表示修改,2表示删除等,shid和shengId是对应到其它表的,如某个城市的邮政编码,主要是用来搜集它们的一些对应的数据而已,这里可不考虑。下面是数据库查询的数据展示图:

2.其次是写一个根据输入的地址,返回省市区路四级联动的数据,然后把第四级路写进Road这个表并且把它的外键FID对应到第三级District区的主键ID

这里使用存储过程来处理,好了,那么问题就来了,执行的地址有好几种情况,在插入路这个数据之前需要对地址进行查找,有五种查找方式:

需要注意城市是否为直辖市(目前直辖市有四个:重庆市,北京市,天津市,上海市)

1)查找方式——省市区路,这个是最理想的,因为地址中已有前面三级的数据,首先找到省,然后找市,再找区,最后找路,如果路不存在就把路插入到Road表,返回省市区路

2)查找方式——省区路,这里只有省和区,首先找出省,然后找区,再根据区找到其所对应的城市,最后找路,如果路不存在就把路插入到Road表,返回省市区路

3)查找方式——省市路,这里只有省和市,于上面两种方式来说是比较复杂,首先找出省,然后找市,再根据路来找出对应的区,如果路不存在就把路插入到Road表,返回省市路

如果存在,则可找出区,返回省市区路。不存在时只能手工更正。

4)查找方式——市区路,这里只有市和区,首先找出市,然后根据市再找省,最后找路,如果路不存在就把路插入到Road表,返回省市区路

5)查找方式——市路,这里只有市,于上面四种方式是最复杂的,首先找出市,如果不是直辖市就找省,然后根据路来找区,如果路不存在就把路插入到Road表,返回省市路

如果存在,则可找出区,返回省市区路。不存在时只能手工更正。

以上是返回省市区路的五种查找方式,下面是存储过程:

GetAddress是查找方式的存储过程,返回省市区路,代码:

 ALTER pROCEDURE [dbo].[GetAddress]
@address varchar(100),
@NProvinceName varchar(30) OUTPUT,
@NCityName varchar(30) OUTPUT,
@NDistrictName varchar(30) OUTPUT,
@NRoadName varchar(50) OUTPUT,
@remain varchar(50) OUTPUT,
@PostCode varchar(7) OUTPUT,
@Road varchar(100) output, --新增,要插入road的路名
@method int output,
@number int output,
@insert bit =1 --新增,是否插入
AS
BEGIN set @address=replace(@address,' ',''); --空格
set @address=replace(@address,' ',''); --制表符
set @address=replace(@address,'0',''); --数字
set @address=replace(@address,'1',''); --数字
set @address=replace(@address,'2',''); --数字
set @address=replace(@address,'3',''); --数字
set @address=replace(@address,'4',''); --数字
set @address=replace(@address,'5',''); --数字
set @address=replace(@address,'6',''); --数字
set @address=replace(@address,'7',''); --数字
set @address=replace(@address,'8',''); --数字
set @address=replace(@address,'9',''); --数字 set @method=1
set @number=0 create table #temp(NProvinceName varchar(30),NCityName varchar(30),NDistrictName varchar(30),
NRoadName varchar(50),remain varchar(50),PostCode varchar(7) ,Road varchar(100),method int,number int) while @method<=5
begin
exec ResAddress @address,@NProvinceName output,@NCityName output,
@NDistrictName output,@NRoadName output,@remain output,@PostCode output,@Road output,@method output,@number output insert into #temp values (@NProvinceName,@NCityName,@NDistrictName,@NRoadName,@remain,@PostCode,@Road,@method,@number) select @NProvinceName=null,@NCityName=null,@NDistrictName=null,@NRoadName=null,@remain=null,@PostCode=null,@number=0,@Road=null
set @method=@method+1
end /*
select top 1 @NProvinceName=NProvinceName,@NCityName=NCityName,@NDistrictName=NDistrictName,
@NRoadName=NRoadName,@remain=remain,@PostCode=PostCode ,@method=method,@number=number,@Road=Road from #temp order by number desc
*/ select top 1 @NProvinceName=dbo.F_Convert(NProvinceName,0),@NCityName=dbo.F_Convert(NCityName,0),@NDistrictName=dbo.F_Convert(NDistrictName,0),
@NRoadName=dbo.F_Convert(NRoadName,0),@remain=dbo.F_Convert(remain,0),@PostCode=dbo.F_Convert(PostCode,0) ,
@method=dbo.F_Convert(method,0),@number=dbo.F_Convert(number,0),@Road=dbo.F_Convert(Road,0) from #temp order by number desc if (@Road is not null) and (@insert=1)
begin
if not exists (select * from road where fid=left(@road,36) and title=RIGHT(@road,LEN(@ROAD)-36) and text=RIGHT(@road,LEN(@ROAD)-36))
insert into Road(fid,title,flag,text) values(left(@road,36),RIGHT(@road,LEN(@ROAD)-36),2,RIGHT(@road,LEN(@ROAD)-36))
end
drop table #temp
END

ResAddress是查找方式中需要调用的存储过程,其中有number这个返回一个数字,数字越大表示查找的难度就越大,代码:

 ALTER pROCEDURE [dbo].[ResAddress]

 @address varchar(100),
@NProvinceName varchar(30) OUTPUT,
@NCityName varchar(30) OUTPUT,
@NDistrictName varchar(30) OUTPUT,
@NRoadName varchar(50) OUTPUT,
@remain varchar(50) OUTPUT,
@PostCode varchar(7) OUTPUT,
@Road varchar(100) output, --新增,要插入road的路名
@method int output,
@number int output AS
BEGIN DECLARE @id varchar(36)
declare @fid varchar(36)
declare @text varchar(30) declare @NAddress varchar(200)
set @NAddress=@address if @method=1 --查找方式 省市区路
begin
select top 1 @NProvinceName=title ,@id=id,@text=text from province where left(@Address,len(text)) like '%'+text+'%' order by len(text) desc
if (@NProvinceName is not null)
begin
set @number=@number+1 --找到省计数加1
select @id=id from province where title=@NProvinceName and shengid is not null
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end select top 1 @NCityName=title,@id=id,@fid=fid ,@text=text from city where left(@Address,len(text)) like '%'+text+'%' and city.fid=@id order by len(text) desc
if (@NCityName is not null)
begin
set @number=@number+1
select @id=id from city where title=@NCityName and shiid is not null
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end select top 1 @NDistrictName=title,@id=id,@fid=fid,@text=text from district where left(@Address,len(text)) like '%'+text+'%' and district.fid=@id order by len(text) desc
if (@NDistrictName is not null)
begin
set @number=@number+1
select @id=id from district where title=@NDistrictName and shiid is not null
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end select top 1 @NRoadName=title,@fid=fid,@id=id ,@PostCode=PostCode,@text=text from Road where left(@Address,len(text)) like '%'+text+'%' and road.fid=@id order by len(text) desc
if (@NRoadName is not null)
begin
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
set @number=@number+1
end if @text is not null
set @remain=right(@NAddress,len(@NAddress)-charindex(@text,@NAddress)-len(@text)+1)
else
set @remain=@Address if (@NRoadName is null) and (@NDistrictName is not null)
goto GetRoad
else
goto finished
end if @method=2 --查找方式 省区路
begin
select top 1 @NProvinceName=title ,@id=id,@text=text from province where left(@Address,len(text)) like '%'+text+'%' order by len(text) desc
if (@NProvinceName is not null)
begin
set @number=@number+1 --找到省计数加1
select @id=id from province where title=@NProvinceName and shengid is not null --找省的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end select top 1 @NDistrictName=title,@id=id,@fid=fid,@text=text from district where left(@Address,len(text)) like '%'+text+'%'
and district.fid in (select id from city where city.fid=@id) order by len(text) desc
if (@NDistrictName is not null)
begin
select @id=id from district where title=@NDistrictName and shiid is not null --找区的标准ID
set @number=@number+1
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end SELECT @NCityName=title FROM City where id=@fid --返回查找市名
if (@NCityName is not null)
set @number=@number+1 select top 1 @NRoadName=title,@fid=fid,@id=id ,@PostCode=PostCode,@text=text from Road where left(@Address,len(text)) like '%'+text+'%'
and Road.fid=@id order by len(text) desc
if (@NRoadName is not null)
begin
set @number=@number+1
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end if @text is not null
set @remain=right(@NAddress,len(@NAddress)-charindex(@text,@NAddress)-len(@text)+1)
else
set @remain=@Address if (@NRoadName is null) and (@NDistrictName is not null)
goto GetRoad
else
goto finished
end if @method=3 --查找方式 省市路
begin
select top 1 @NProvinceName=title ,@id=id,@text=text from province where left(@Address,len(text)) like '%'+text+'%' order by len(text) desc
if (@NProvinceName is not null)
begin
set @number=@number+1 --找到省计数加1
select @id=id from province where title=@NProvinceName and shengid is not null --找省的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end if @NProvinceName in ('重庆市','北京市','天津市','上海市') --新增这个判断 2009-08-25
begin
select top 1 @NCityName=title,@id=id,@fid=fid,@text=text from city where city.fid=@id
set @number=@number+1
end
else
begin
select top 1 @NCityName=title,@id=id,@fid=fid,@text=text from city where left(@Address,len(text)) like '%'+text+'%'
and city.fid=@id order by len(text) desc
if (@NCityName is not null)
begin
set @number=@number+1
select @id=id from city where title=@NCityName and shiid is not null --找市的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end
end select top 1 @NRoadName=title,@fid=fid,@id=id ,@PostCode=PostCode,@text=text from Road where left(@Address,len(text)) like '%'+text+'%'
and road.fid in (select id from district where district.fid=@id) order by len(text) desc
if (@NRoadName is not null)
begin
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
set @number=@number+1
end select @NDistrictName=title from district where id=@fid --返回查找区名
if (@NDistrictName is not null)
set @number=@number+1 if @text is not null
set @remain=right(@NAddress,len(@NAddress)-charindex(@text,@NAddress)-len(@text)+1)
else
set @remain=@Address
if (@NRoadName is null) and (@NDistrictName is not null)
goto GetRoad
else
goto finished
end if @method=4 --查找方式 市区路
begin
select top 1 @NCityName=title,@id=id,@fid=fid ,@text=text from city where left(@Address,len(text)) like '%'+text+'%' order by len(text) desc
if (@NCityName is not null)
begin
select @id=id from city where title=@NCityName and shiid is not null --找市的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
set @number=@number+1
end select @NProvinceName=title from Province where id=@fid --返回找省名
if (@NProvinceName is not null)
begin
set @number=@number+1
end select top 1 @NDistrictName=title,@id=id,@fid=fid, @text=text from district where left(@Address,len(text)) like '%'+text+'%'
and district.fid=@id order by len(text) desc
if (@NDistrictName is not null)
begin
set @number=@number+1
select @id=id from district where title=@NDistrictName and shiid is not null --找区的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end select top 1 @NRoadName=title,@fid=fid,@id=id ,@PostCode=PostCode,@text=text from Road where left(@Address,len(text)) like '%'+text+'%'
and road.fid=@id order by len(text) desc
if (@NRoadName is not null)
begin
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
set @number=@number+1
end if @text is not null
set @remain=right(@NAddress,len(@NAddress)-charindex(@text,@NAddress)-len(@text)+1)
else
set @remain=@Address if (@NRoadName is null) and (@NDistrictName is not null)
goto GetRoad
else
goto finished
end if @method=5 --查找方式 :市-路
begin
select top 1 @NCityName=title,@id=id,@fid=fid , @text=text from city where left(@Address,len(text)) like '%'+text+'%' order by len(text) desc
if (@NCityName is not null)
begin
set @number=@number+1
select @id=id from city where title=@NCityName and shiid is not null --找市的标准ID
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
end
select @NProvinceName=title from Province where id=@fid --返回找省名
if (@NProvinceName is not null)
set @number=@number+1 select top 1 @NRoadName=title,@fid=fid,@id=id ,@PostCode=PostCode ,@text=text from Road where left(@Address,len(text)) like '%'+text+'%'
and road.fid in (select id from district where district.fid=@id) order by len(text) desc
if (@NRoadName is not null)
begin
set @Address=stuff(@Address,charindex(@text,@Address),LEN(@text),'')
set @number=@number+1
end select @NDistrictName=title from district where id=@fid --返回查找区名
if (@NDistrictName is not null)
set @number=@number+1 if @text is not null
set @remain=right(@NAddress,len(@NAddress)-charindex(@text,@NAddress)-len(@text)+1)
else
set @remain=@Address if (@NRoadName is null) and (@NDistrictName is not null)
goto GetRoad
else
goto finished
end GetRoad: --新增将返回@road
if charindex('路',@Address)>0
set @Road=left(@address,charindex('路',@Address))
else if charindex('道',@Address)>0
set @Road=left(@address,charindex('道',@Address))
else if charindex('街',@Address)>0
set @Road=left(@address,charindex('街',@Address))
else if charindex('镇',@Address)>0
set @Road=left(@address,charindex('镇',@Address))
else if charindex('乡',@Address)>0
set @Road=left(@address,charindex('乡',@Address))
else if charindex('屯',@Address)>0
set @Road=left(@address,charindex('屯',@Address))
else if charindex('村',@Address)>0
set @Road=left(@address,charindex('村',@Address))
else if charindex('社',@Address)>0
set @Road=left(@address,charindex('社',@Address)) set @road=@id+@road
finished:
END

execAddress是执行存储过程,根据输入的地址返回详细的地址,代码:

 ALTER Procedure [dbo].[execAddress]
@inputaddress varchar(100)
AS
DECLARE @return_value int,
@NProvinceName varchar(30),
@NCityName varchar(30),
@NDistrictName varchar(30),
@NRoadName varchar(50),
@remain varchar(50),
@PostCode varchar(7),
@Road varchar(100),
@method int,
@number int EXEC @return_value = [dbo].[GetAddress]
@address = @inputaddress,
@NProvinceName = @NProvinceName OUTPUT,
@NCityName = @NCityName OUTPUT,
@NDistrictName = @NDistrictName OUTPUT,
@NRoadName = @NRoadName OUTPUT,
@remain = @remain OUTPUT,
@PostCode = @PostCode OUTPUT,
@Road = @Road OUTPUT,
@method = @method OUTPUT,
@number = @number OUTPUT

以下是测试,根据输入的地址返回省市区路的详细地址,这里就只截图:

1.输入重庆新华路41-43号,点转换,执行存储过程execAddress,把返回的地址显示在输出地址里,如下图

这里是第五种查找方式(市路)也可能是第三种查找方式(省市路)。

2.输入广州海珠区广州大道客村墩和,返回广东省广州市海珠区广州大道客村墩和,如下图:

这里是第四种查找方式(市区路)。

以上就是介绍查找地址的方法,还没用到address表,这个表是前面所说的用来搜集有详细地址的,具体需要写一个网页爬虫的程序,去爬爬有地址的网站,如大众点评网,这里就介绍这么多了,有写得不好的还望各位手下留情~

sql地址寻路算法(省市区路)的更多相关文章

  1. 【转载】 A* 寻路算法 (个人认为最详细,最通俗易懂的一个版本)

    原文地址: http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html =============================== ...

  2. A星寻路算法-Mind&Hand(C++)

    //注1:Mind & Hand,MIT校训,这里指的理解与实现(动脑也动手) //注2:博文分为两部分:(1)理解部分,为参考其他优秀博文的摘要梳理:(2)代码部分,是C++代码实现的,源码 ...

  3. A* 寻路算法[转载]

    A* 寻路算法 转载地址:http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html 原文地址: http://www.gamedev ...

  4. 三角网格上的寻路算法Part.1—Dijkstra算法

    背景 最近在研究中产生了这样的需求:在三角网格(Mesh)表示的地形图上给出两个点,求得这两个点之间的地面距离,这条距离又叫做"测地线距离(Geodesic)".计算三角网格模型表 ...

  5. A星寻路算法介绍

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...

  6. A*寻路算法

    对于初学者而言,A*寻路已经是个比较复杂的算法了,为了便于理解,本文降低了A*算法的难度,规定只能横竖(四方向)寻路,而无法直接走对角线,使得整个算法更好理解. 简而言之,A*寻路就是计算从起点经过该 ...

  7. js实现A*寻路算法

    这两天在做百度前端技术学院的题目,其中有涉及到寻路相关的,于是就找来相关博客进行阅读. 看了Create Chen写的理解A*寻路算法具体过程之后,我很快就理解A*算法的原理.不得不说作者写的很好,通 ...

  8. 用简单直白的方式讲解A星寻路算法原理

    很多游戏特别是rts,rpg类游戏,都需要用到寻路.寻路算法有深度优先搜索(DFS),广度优先搜索(BFS),A星算法等,而A星算法是一种具备启发性策略的算法,效率是几种算法中最高的,因此也成为游戏中 ...

  9. A*寻路算法的探寻与改良(一)

    A*寻路算法的探寻与改良(一) by:田宇轩                                                                    第一部分:这里我们主 ...

随机推荐

  1. 51Nod 1717

    链接 分析:对于任意一个数,它的约数总是成对出现的,但是对于完全平方数,它因为有两个约数不相等,所以只会出现奇数次,所以最终的结果就是减去完全平方数 #include "iostream&q ...

  2. 【前端】CentOS 7 系列教程之三: 搭建 git 服务器

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_3.html 上一篇我们安装好了git,这一篇我们搭建git服务器 创建一个用户组 groupadd g ...

  3. ie7 ie8 使用border模拟圆

    border-radius 属性ie8+才支持,ie7 ie8 下的圆角就可以使用border进行模拟:(移动端都支持) 我们平常使用border-style一般都是solid实线,其他常用的还有da ...

  4. git只clone仓库中指定子目录

    基于sparse clone变通方法 [root@vm_test backup]# mkdir devops[root@vm_test backup]# cd devops/[root@vm_test ...

  5. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  6. C++开发工程师面试题库 150~200道

    151.简述需求分析的过程和意义 152.网状.层次数据模型与关系数据模型的最大的区别是什末 153.软件质量保证体系是什末 国家标准中与质量保证管理相关的几个标准是什末 编号和全称是什末号和全称是什 ...

  7. hdoj5327【前缀和思想】

    题意: 找给定区间的美丽数,美丽数的意思就是这个数每个位上的数都是唯一的. 思路: 前缀和的思想. 感想: 就是你当前位置代表某个特性的前面的所有和(瞎比比的,说了下感觉).前提是你必须找到这样的特性 ...

  8. python 元类 type metaclass

    python中一切皆对象,类对象创建实例对象,元类创建类对象,元类创建元类. 元类创建类对象有2中方式: 一.type方法 type(类名, 由父类名称组成的元组(针对继承的情况,可以为空),包含属性 ...

  9. bzoj 4504: K个串【大根堆+主席树】

    像超级钢琴一样把五元组放进大根堆,每次取一个出来拆开,(d,l,r,p,v)表示右端点为d,左端点区间为(l,r),最大区间和值为v左端点在p上 关于怎么快速求区间和,用可持久化线段树维护(主席树?) ...

  10. 一道简单的 Java 笔试题,但值得很多人反思!

    前言 面试别人,对我来说是一件新奇事,以前都是别人面试我.我清楚地知道,我在的地域与公司,难以吸引到中国的一流软件人才.所以,我特地调低了期望,很少问什么深入的技术问题,只问一些广泛的.基础的.我只要 ...