//alter load_map.dev
//safety verion 2016/1/12
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include<sstream> //使用istringstream必须包含的头文件
#include<string>
#include "string2num.hpp"
#include "map.hpp"
#include <windows.h>
#include <GL/glut.h>
using namespace std; void test_map(){ unsigned int sum;double Sa;
double north=polys[]->north();
double south=polys[]->south();
double east=polys[]->east();
double west=polys[]->west();
for(int i=;i<polys.size();i++){
sum+=polys[i]->points.size();
Sa+=polys[i]->area();
}
for(int i=;i<polys.size();i++){
//比较每一个polygon的边界值,求出整个地图的四个边界值
if(polys[i]->north()>=north)north=polys[i]->north();
if(polys[i]->south()<=south)south=polys[i]->south();
if(polys[i]->east()>=east)east=polys[i]->east();
if(polys[i]->west()<=west)west=polys[i]->west();
} ofstream out("map_para.txt");
if(out.is_open())
{
out <<"map parameter:\n";
out<<"count polygon="<<polys.size()<<endl;
out<<"size="<<sum<<endl;
out<<"area="<<Sa<<endl;
out<<"north="<<north<<endl;
out<<"south="<<south<<endl;
out<<"east="<<east<<endl;
out<<"west="<<west<<endl;
out.close();
}
} void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//用蓝色色绘制各省边界
glColor3f (0.0, 0.0, 1.0);
glPolygonMode(GL_BACK, GL_LINE);
for(int i=;i<polys.size();i++)
{
vector<MapPoint> points=polys[i]->points;
glBegin(GL_LINE_STRIP);
for(int j=;j<points.size();j++)
{
glVertex3f (points[j].longitude, points[j].latitude, 0.0);
}
glEnd();
}
glFlush();
}
void init (void)
{
//设置背景颜色
glClearColor (, 1.0, , 0.0);
//初始化观察值
glMatrixMode(GL_PROJECTION); //将矩阵模式设为投影
glLoadIdentity(); //对矩阵进行单位化
glOrtho(110.0, 118.0, 30.0, 38.0, -1.0, 1.0); //构造平行投影矩阵
} int main(int argc, char *argv[]){
//数据文件请到http://files.cnblogs.com/opengl/HenanCounty.rar下载放到D盘根目录下并解压
string filename="HenanCounty.txt";//在当前工程目录下
// ReadData2num(filename);
polys=ReadMapData(filename);
test_map(); glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //单缓存和RGB
glutInitWindowSize (, );
glutInitWindowPosition (, );
glutCreateWindow ("Map_henan");
init ();
glutDisplayFunc(display); //显示回调函数
glutMainLoop(); return ;
}
 
"HenanCounty.txt"是一份文本格式的地图:

单个整数代表点数(包含的点可能是某个省内市区的范围),整数n下面紧接着的数据行共有n行,是以经纬度表示的地理坐标。
程序运行结果:

关于地图的信息(多边形数目、总点数、面积、边界)通过test_map()函数写入map_para.txt

目前算得的面积还有问题,这通过边界值就可以看出来,这还有待解决。

string2num.hpp定义了模板函数用于字符串形式的数字向基本数值类型的转化(其实这个定义比较多余<sstream>里面定义的字符串处理类包含此功能)

#ifndef _STRING2NUM_HPP_
#define _STRING2NUM_HPP_
#include<sstream> //使用istringstream必须包含的头文件
#include<string>
using namespace std;
//模板函数:将string类型变量转换为常用的数值类型 by maowei
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss>>num;
return num;
} #endif

map.hpp包括了基本类的定义,这是在map_origin.cpp的基础上修改得到的,主要是增加了一些获取地图信息相关的函数。其中多边形容器的定义部分值得充分学习消化。

#ifndef _MAP_HPP_
#define _MAP_HPP_
#include <vector>
#include <math.h>
using namespace std;
class MapPoint
{
public:
double longitude;//经度
double latitude;//纬度
MapPoint(){}
MapPoint(double x,double y){longitude=x;latitude=y;}
};
class Map
{
public:
int mapsize;
vector<MapPoint> points; //多边形的顶点序列
Map(){}
Map(int i){mapsize=i;}
}; //unsigned int count,num;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%translate from origin
class Polygon
{
public:
vector<MapPoint> points; //多边形的顶点序列
double area(void){
double A=;unsigned int N=points.size();
for(int i=;i<(N-);i++){
A+=fabs(points[i].longitude*points[i+].latitude-points[i].latitude*points[i+].longitude);
}
A+=fabs(points[N-].longitude*points[].latitude-points[N-].latitude*points[].longitude);
return A/;
}
double north(void){
double N=points[].latitude;
for(int i=;i<points.size();i++){if(points[i].latitude>=N)N=points[i].latitude; }
return N;
}
double south(void){
double S=points[].latitude;
for(int i=;i<points.size();i++){if(points[i].latitude<=S)S=points[i].latitude; }
return S;
}
double east(void){
double E=points[].longitude;
for(int i=;i<points.size();i++){if(points[i].longitude>=E)E=points[i].longitude; }
return E;
}
double west(void){
double W=points[].longitude;
for(int i=;i<points.size();i++){if(points[i].longitude<=W)W=points[i].longitude; }
return W;
}
};
vector<Polygon*> polys; //多边形集合
vector<Polygon*> ReadMapData(const string filename)
{
int PointCount;
vector<Polygon*> polygons;
ifstream fs(filename.c_str());
while(fs.eof()!=true)
{
Polygon* poly=new Polygon;
fs>>PointCount;
// cout<<PointCount<<endl;
for(int i=;i<PointCount;i++)
{
MapPoint p;
fs>>p.longitude>>p.latitude;
poly->points.push_back(p);
}
polygons.push_back(poly); }
return polygons;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Map ReadData2num(const string str) //逐行读取 并转化为常用数据类型 by mememagic
{
cout<<"start read map"<<endl;
ifstream fin(str.c_str());
if (! fin.is_open())
{ cout << "Error opening file"; exit (); }
string s; Map Imap;
int Size=,i=;bool flag;
while( getline(fin,s) )
{
if(i==Size){int s_i=stringToNum<int>(s);Size=s_i+;i=;//count+=s_i;num++;
//cout << "(vertex num): " << s_i << endl;
}
else
{
istringstream is(s);//采用istringstream从string对象str中读取字符
string s1;double x,y;
while(is>>s1){
double s_d=stringToNum<double>(s1);
if(!flag){x=s_d;flag=!flag; }
else{y=s_d;flag=!flag; }
//cout<<s_d<<' ';
}
//cout<<"x="<<x<<' '<<"y="<<y<<endl;
MapPoint p(x,y);
Imap.points.push_back(p);
}
i++;
}
return Imap;
} #endif

与Draw_v1相比,这个程序有了不少变化:数据来源不再靠手工在命令窗口输入,而是一个有确定“格式”的简单文本,数据量也较大,程序里面对数据的处理代码自然不同;多边形容器(vector<Polygon*> polys )是对图形(vector<Shape>)容器的拓展;利用OpenGL实现了数据的可视化(关于OpenGL绘图的原理有待进一步学习理解).

												

Draw_extend使用OpenGL显示数据点的更多相关文章

  1. ZingChart 隐藏数据点

    正常情况下 zingChart 的数据点会显示到图表中,但是如果数据点很多的情况下,可能会让你无法准确的预测趋势,而且也不美观 在 js 配置中添加最多允许显示的数据点,超过这个值将不显示数据点 效果 ...

  2. 理解数据点,自变量和因变量(参数和值)ChartControl

    WinForms Controls > Controls > Chart Control > Fundamentals > Charting Basics > Under ...

  3. Keil UV4 BUG(带字库液晶不能显示“数、正、过”问题的请看)

    Keil UV3一直存在汉字显示(0xFD)的bug,以前在用到带字库的12864液晶的时候,“数”字总是不能正常显示,后来有网友告诉我这是keil的bug,解决掉了.后来keil升级了,我也换了新版 ...

  4. OpenGL显示图片

    最近想用C++在windows下实现一个基本的图像查看器功能,目前只想到了使用GDI或OpenGL两种方式.由于实在不想用GDI的API了,就用OpenGL的方式实现了一下基本的显示功能. 用GDAL ...

  5. 第12课 OpenGL 显示列表

    显示列表: 想知道如何加速你的OpenGL程序么?这一课将告诉你如何使用OpenGL的显示列表,它通过预编译OpenGL命令来加速你的程序,并可以为你省去很多重复的代码. 这次我将教你如何使用显示列表 ...

  6. android linphone中opengl显示的实现

    1,java层 在界面中创建GL2JNIView(基类为GLSurfaceView). 创建对象AndroidVideoWindowImpl,将GL2JNIView作为参数传入构造函数.在该对象中监听 ...

  7. VS+OpenGl 显示三维STL模型 代码

    今天调出了用VS环境结合OpenGL glut工具包进行显示STL模型的模块,进行了渲染.效果: 如下,后期会进行进一步优化,先贴上: #ifndef DATA_H #define DATA_H st ...

  8. [记录]使用openGL显示点云的一个程序

    #include <GL/glut.h> #include <stdio.h> #include <iostream> using namespace std; v ...

  9. OPENGL 显示BMP图片+旋转

    VS2010/Windows 7/ 1. 需包含头文件 stdio.h, glaux.h, glut.h.需要对应的lib,并添加包含路径 2. 窗口显示用glut库的函数 3. bmp图片从本地读取 ...

随机推荐

  1. 模板列onclick事件中绑定跳转页参数(onclick location.href Eval)

    <asp:GridView runat="server" ID="gvCheckList" AutoGenerateColumns="false ...

  2. windows下登录lunix服务器

    在微信项目中,负责发布,我就把我用到的记录一下.有两种登录方式,看你要做什么操作. 1.SecureCRT 支持命令行操作.(主要是发布程序) 调试.微信公众号规定要有服务器的网址,一般公司的都是内网 ...

  3. 在代码设置RelativeLayout的属性,比如layout_below

    ( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...

  4. Linux虚拟机中 Node.js 开发环境搭建

    Node.js 开发环境搭建: 1.下载CentOS镜像文件和VMWare虚拟机程序; 2.安装VMWare——>添加虚拟机——>选择CentOS镜像文件即可默认安装带有桌面的Linux虚 ...

  5. Node.js的cluster模块——Web后端多进程服务

    众所周知,Node.js是单线程的,一个单独的Node.js进程无法充分利用多核.Node.js从v0.6.0开始,新增cluster模块,让Node.js开发Web服务时,很方便的做到充分利用多核机 ...

  6. PLSQL大数据生成规则

    数据定义 数据定义决定了被生成的数据.如果要创建简单的字符,可以在两个方括号之间输入字符定义:[数据] 数据可以是下列预先确定的集的混合体:           •  a: a..z (小写字符)   ...

  7. 如何通过命令行创建和设置一个MySQL用户

    我想要在MySQL服务器上创建一个新的用户帐号,并且赋予他适当的权限和资源限制.如何通过命令行的方式来创建并且设置一个MySQL用户呢? 要访问一个MySQL服务器,你需要使用一个用户帐号登录其中方可 ...

  8. WCF传输大数据的设置

    在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于 ...

  9. Python学习06——列表的操作(2)

    笨办法学Python第39节 之前用的第三版的书,昨天发现内容不对,八块腹肌又给我下了第四版,这次的内容才对上.本节的代码如下: ten_things = "Apples Oranges C ...

  10. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...