//
void read(const char* input_path,const char* pLayerName,int my_rank,int group_size)
{
//puts("[INFO] READING...");
//输入的参数input_path :db 127.0.0.1 postgres ******** 5432 beijing result
// pLayerName数据表名
// my_rank:进程序号
// group_size:进程数
//作用:将postgres中的数据按照分配到各个进程,如21条矢量数据、4个进程
//进程0:1-6号的数据;进程1:7-11号数据;进程2:12-16号数据;进程3:17-21号数据
OGRDataSource *pdataset = OGRSFDriverRegistrar::Open(input_path, FALSE);
if (!pdataset)
{
fprintf(stderr, "[ERROR] input path is not valid.\n");
return;
}
OGRLayer *player = pdataset->GetLayerByName(pLayerName);
int ElementNun=player->GetFeatureCount();
//cout<<"ElementNun="<<ElementNun<<endl;
//player->GetFeatureCount();
if (!player)
{
fprintf(stderr, "[ERROR] input layer is not valid.\n");
return;
}
OGRSpatialReference* psrs = player->GetSpatialRef();
if (psrs)
{
srs = *psrs;
}
player->GetExtent(&rect, FALSE);
double global_width = rect.MaxX - rect.MinX;
double global_height = rect.MaxY - rect.MinY;
double scale_width = (double) width / global_width;
double scale_height = (double) height / global_height;
if (scale_width > scale_height)
{
scale = scale_height;
width = (int) (scale * global_width);
}
else
{
scale = scale_width;
height = (int) (scale * global_height);
}
int lstrings_size = lstrings.size();
//cout<<"lstrings_size="<<lstrings_size<<endl;
int delti(0),tempi(0),tempmy_rank(0);
if (my_rank<ElementNun%group_size)
{
delti=my_rank;
tempi=1;
}
else
{
delti=ElementNun%group_size;
tempi=0;
}
if (my_rank<=group_size-1)
{
tempmy_rank=1;
}
else
{
tempmy_rank=0;
}
OGRFeature *pfeature = player->GetFeature(my_rank*ElementNun/group_size+delti+1);
int BeginIDEveryRank=my_rank*int(ElementNun/group_size)+delti+1;
int NunEleEveryRank=int(ElementNun/group_size)-1+tempi+tempmy_rank;
cout<<"Rank="<<my_rank<<endl;
//cout<<"NunEleEveryRank="<<NunEleEveryRank<<endl;
cout<<"BeginIDEveryRank="<<BeginIDEveryRank<<endl;
int BreakWhile(0);
while (pfeature != NULL&&BreakWhile<NunEleEveryRank)
{
OGRGeometry *pgeom = pfeature->GetGeometryRef();
if (!pgeom)
{
fprintf(stderr,
"[WARN] 1 feature without geometry is ignored.\n");
continue;
}
OGRwkbGeometryType gtype = pgeom->getGeometryType();
if (gtype == wkbLineString)
{
OGRLineString *polstr = static_cast<OGRLineString *>(pgeom);
LineString lstring;
lstring.initWith(polstr, rect, scale);
lstrings.push_back(lstring);
BreakWhile++;
}
else if (gtype == wkbMultiLineString)
{
OGRMultiLineString *pomlstr =
static_cast<OGRMultiLineString *>(pgeom);
int ngeoms = pomlstr->getNumGeometries();
for (int i = 0; i < ngeoms; i++)
{
OGRLineString *polstr =
static_cast<OGRLineString *>(pomlstr->getGeometryRef(
i));
LineString lstring;
lstring.initWith(polstr, rect, scale);
lstrings.push_back(lstring);
BreakWhile++;
}
}
else
{
fprintf(stderr, "[WARN] 1 feature (type is %d) is ignored.\n",
gtype);
continue;
}
OGRFeature::DestroyFeature(pfeature);
// pfeature = player->GetNextFeature(); 不用GetNextFeature(),因为在读//postgresql数据库时有可能乱码。
pfeature = player->GetFeature(BeginIDEveryRank+BreakWhile-1);
}
cout<<"BreakWhile="<<BreakWhile+BeginIDEveryRank-1<<endl;
OGRDataSource::DestroyDataSource(pdataset);
}
MPI多进程杜postgresql矢量数据库函数
最新推荐文章于 2024-02-23 21:48:52 发布