示例#1
0
int ILI2Reader::AddFeature(DOMElement *elem) {
  bool newLayer = true;
  OGRLayer *curLayer = NULL;
  char *pszName = tr_strdup(elem->getTagName());
  //CPLDebug( "OGR_ILI", "Reading layer: %s", pszName );

  // test if this layer exist
  curLayer = GetLayer(pszName);
  newLayer = (curLayer == NULL);

  // add a layer
  if (newLayer) {
    CPLDebug( "OGR_ILI", "Adding layer: %s", pszName );
    OGRFeatureDefn* poFeatureDefn = new OGRFeatureDefn(pszName);
    poFeatureDefn->SetGeomType( wkbUnknown );
    GeomFieldInfos oGeomFieldInfos;
    curLayer = new OGRILI2Layer(poFeatureDefn, oGeomFieldInfos, NULL);
    m_listLayer.push_back(curLayer);
  }

  // the feature and field definition
  OGRFeatureDefn *featureDef = curLayer->GetLayerDefn();
  if (newLayer) {
    // add TID field
    OGRFieldDefn ofieldDefn (ILI2_TID, OFTString);
    featureDef->AddFieldDefn(&ofieldDefn);

    setFieldDefn(featureDef, elem);
  }

  // add the features
  OGRFeature *feature = new OGRFeature(featureDef);

  // assign TID
  int fIndex = feature->GetFieldIndex(ILI2_TID);
  if (fIndex != -1) {
      XMLCh *pszIli2_tid = XMLString::transcode(ILI2_TID);
      char *fChVal = tr_strdup(elem->getAttribute(pszIli2_tid));
      feature->SetField(fIndex, fChVal);
      XMLString::release(&pszIli2_tid);
      CPLFree(fChVal);
  } else {
      CPLDebug( "OGR_ILI","'%s' not found", ILI2_TID);
  }

  SetFieldValues(feature, elem);
  CPL_IGNORE_RET_VAL(curLayer->SetFeature(feature));

  CPLFree(pszName);

  return 0;
}
示例#2
0
int ILI2Reader::AddFeature(DOMElement *elem) {
  bool newLayer = true;
  OGRLayer *curLayer = 0;
  char *pszName = XMLString::transcode(elem->getTagName());

  // test if this layer exist
  for (list<OGRLayer *>::reverse_iterator layerIt = m_listLayer.rbegin();
       layerIt != m_listLayer.rend();
       ++layerIt) {
    OGRFeatureDefn *fDef = (*layerIt)->GetLayerDefn();
    if (cmpStr(fDef->GetName(), pszName) == 0) {
      newLayer = false;
      curLayer = *layerIt;
      break;
    }
  }

  // add a layer
  if (newLayer) { // FIXME in Layer: SRS Writer Type datasource
    CPLDebug( "OGR_ILI", "Adding layer: %s", pszName );
    // new layer data
    OGRSpatialReference *poSRSIn = NULL; // FIXME fix values for initial layer
    int bWriterIn = 0;
    OGRwkbGeometryType eReqType = wkbUnknown;
    OGRILI2DataSource *poDSIn = NULL;
    curLayer = new OGRILI2Layer(pszName, poSRSIn, bWriterIn, eReqType, poDSIn);
    m_listLayer.push_back(curLayer);
  }

  // the feature and field definition
  OGRFeatureDefn *featureDef = curLayer->GetLayerDefn();
  if (newLayer) {
    // add TID field
    OGRFieldDefn ofieldDefn (ILI2_TID, OFTString);
    featureDef->AddFieldDefn(&ofieldDefn);

    setFieldDefn(featureDef, elem);
  }

  // add the features
  OGRFeature *feature = new OGRFeature(featureDef);

  // assign TID
  int fIndex = feature->GetFieldIndex(ILI2_TID);
  if (fIndex != -1) {
      XMLCh *pszIli2_tid = XMLString::transcode(ILI2_TID);
      char *fChVal = XMLString::transcode(elem->getAttribute(pszIli2_tid));
      feature->SetField(fIndex, fChVal);
      XMLString::release (&pszIli2_tid);
      XMLString::release (&fChVal);
  } else {
      CPLDebug( "OGR_ILI","'%s' not found", ILI2_TID);
  }

  SetFieldValues(feature, elem);
  curLayer->SetFeature(feature);
  
  XMLString::release (&pszName);

  return 0;
}
示例#3
0
CPLErr GNMGenericNetwork::ChangeAllBlockState(bool bIsBlock)
{
    if(!m_bIsGraphLoaded && LoadGraph() != CE_None)
    {
        return CE_Failure;
    }

    OGRFeature *poFeature;
    m_poGraphLayer->ResetReading();
    while ((poFeature = m_poGraphLayer->GetNextFeature()) != NULL)
    {
        if(bIsBlock)
        {
            poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_ALL );
        }
        else
        {
            poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_NONE );
        }

        if( m_poGraphLayer->SetFeature( poFeature ) != OGRERR_NONE )
        {
            OGRFeature::DestroyFeature( poFeature );
            CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
            return CE_Failure;
        }

        OGRFeature::DestroyFeature( poFeature );
    }

    // change all network layers

    for(size_t i = 0; i < m_apoLayers.size(); ++i)
    {
        OGRLayer* poLayer = m_apoLayers[i];
        if(NULL == poLayer)
            continue;
        while ((poFeature = poLayer->GetNextFeature()) != NULL)
        {
            if(bIsBlock)
            {
                poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_ALL );
            }
            else
            {
                poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_NONE );
            }

            if( poLayer->SetFeature( poFeature ) != OGRERR_NONE )
            {
                OGRFeature::DestroyFeature( poFeature );
                CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
                return CE_Failure;
            }

            OGRFeature::DestroyFeature( poFeature );
        }
    }

    m_oGraph.ChangeAllBlockState(bIsBlock);

    return CE_None;
}
示例#4
0
CPLErr GNMGenericNetwork::ChangeBlockState(GNMGFID nFID, bool bIsBlock)
{
    if(!m_bIsGraphLoaded && LoadGraph() != CE_None)
    {
        return CE_Failure;
    }

    // change block state in layer
    OGRLayer* poLayer = GetLayerByName(m_moFeatureFIDMap[nFID]);
    if(NULL == poLayer)
    {
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to get layer '%s'.",
                  m_moFeatureFIDMap[nFID].c_str() );
        return CE_Failure;
    }

    OGRFeature* poFeature = poLayer->GetFeature(nFID);
    if(NULL == poFeature)
    {
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to get feature '"
                  GNMGFIDFormat"'.", nFID );
        return CE_Failure;
    }

    if(bIsBlock)
    {
        poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_ALL );
    }
    else
    {
        poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_NONE );
    }

    if( poLayer->SetFeature( poFeature ) != OGRERR_NONE )
    {
        OGRFeature::DestroyFeature( poFeature );
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
        return CE_Failure;
    }

    OGRFeature::DestroyFeature( poFeature );

    GNMGFID nSrcFID, nTgtFID, nConFID;

    // change block state in graph layer
    m_poGraphLayer->ResetReading();
    while ((poFeature = m_poGraphLayer->GetNextFeature()) != NULL)
    {
        nSrcFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_SOURCE);
        nTgtFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_TARGET);
        nConFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_CONNECTOR);
        int nBlockState = poFeature->GetFieldAsInteger(GNM_SYSFIELD_BLOCKED);

        if(bIsBlock)
        {
            if(nSrcFID == nFID)
                nBlockState |= GNM_BLOCK_SRC;
            else if(nTgtFID == nFID)
                nBlockState |= GNM_BLOCK_TGT;
            else if(nConFID == nFID)
                nBlockState |= GNM_BLOCK_CONN;

            poFeature->SetField( GNM_SYSFIELD_BLOCKED, nBlockState );
        }
        else
        {
            if(nSrcFID == nFID)
                nBlockState &= ~GNM_BLOCK_SRC;
            else if(nTgtFID == nFID)
                nBlockState &= ~GNM_BLOCK_TGT;
            else if(nConFID == nFID)
                nBlockState &= ~GNM_BLOCK_CONN;

            poFeature->SetField( GNM_SYSFIELD_BLOCKED, nBlockState );
        }

        if( m_poGraphLayer->SetFeature( poFeature ) != OGRERR_NONE )
        {
            OGRFeature::DestroyFeature( poFeature );
            CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
            return CE_Failure;
        }

        OGRFeature::DestroyFeature( poFeature );
    }

    // change block state in graph
    m_oGraph.ChangeBlockState(nFID, bIsBlock);

    return CE_None;
}
void DataCvt_Log::CalAsShp()
{
	double ptmptime_start = MPI_Wtime();
	OGRRegisterAll();
	//create a new shp to caculate
	CBaseOperate pbaseOperate;
	if(rankid==0)
	{
	     pbaseOperate.CreateCopyShp(m_DatasourceConStr.c_str(),m_PathOrTableName.c_str(),m_ResultPathOrTableName.c_str());
	}
	MPI_Barrier(MPI_COMM_WORLD);
	string pLayerName = m_ResultPathOrTableName;
	if(strcmp(m_DatasourceConStr.c_str(),"")==0)//input is shp file
	{
		m_DatasourceConStr = m_ResultPathOrTableName;
		string pShpPath = m_ResultPathOrTableName;
		string p_LayerName = GetFileNameOnly(pShpPath.c_str());
		int pindex = p_LayerName.find_first_of('.');
		pLayerName = p_LayerName.erase(pindex,p_LayerName.size()-1);
	}

	OGRDataSource* poDS = OGRSFDriverRegistrar::Open(m_DatasourceConStr.c_str(),TRUE); 
	OGRLayer* poLayer = poDS->GetLayerByName(pLayerName.c_str());
	OGRFeature * pFeature =poLayer->GetNextFeature();

	m_RowNum = poLayer->GetFeatureCount();
	//if -c argv is null, all field will be used
	m_ColNum= pFeature->GetFieldCount();
	if(c_CaculateCols.size()==0)
	{
		if(rankid==0)
			cout<<"!!!!!!!  No input columns ids, all fields in the input file will be used..."<<endl<<endl;
		for(int i=0;i<m_ColNum;i++)
		{
			c_CaculateCols.push_back(i);
		}
	}

	double ptmptime_end = MPI_Wtime();
	m_GdalIOInCal_Time=m_GdalIOInCal_Time+ptmptime_end-ptmptime_start;

	//assign number to each process
	int pnum_start,pnum_end;
	int pMyProcessNum=0; int pRemainder=m_RowNum%numproc;
	if(rankid<pRemainder)
	{
		pMyProcessNum = m_RowNum/numproc+1;
		pnum_start = rankid*pMyProcessNum;
		pnum_end = pnum_start+pMyProcessNum-1;
	}
	else
	{
		pMyProcessNum = m_RowNum/numproc;
		pnum_start = pRemainder*(pMyProcessNum+1)+(rankid-pRemainder)*pMyProcessNum;
		pnum_end = pnum_start+pMyProcessNum-1;
	}

	//postgis: fid begins from 1, not 0
	string pwhere="";
	if(strcmp(m_DatasourceConStr.substr(0,3).c_str(),"PG:")==0)//input is postgis
	{
		pwhere = "gid>="+toString(pnum_start+1)+" and gid<="+toString(pnum_end+1);
	}
	else//shpfile: fid begins from 0, not 1
	{
		pwhere = "fid>="+toString(pnum_start)+" and fid<="+toString(pnum_end);
	}

	poLayer->SetAttributeFilter(pwhere.c_str());
	pFeature = poLayer->GetNextFeature();
	while(pFeature!=NULL)
	{		
		for(int i=0;i<c_CaculateCols.size();i++)
		{			
			double tmp = pFeature->GetFieldAsDouble(c_CaculateCols[i]);
			if(tmp!=0)
				tmp = log(fabs(tmp));
			else
				tmp=0;
			double pstart = MPI_Wtime();
			pFeature->SetField(c_CaculateCols[i],tmp);
			poLayer->SetFeature(pFeature);
			double pend = MPI_Wtime();
			m_GdalIOInCal_Time = m_GdalIOInCal_Time+pend-pstart;
		}		
		pFeature = poLayer->GetNextFeature();
	}
	OGRDataSource::DestroyDataSource( poDS );
	double ptmptime_endall = MPI_Wtime();
	Mpi_Caculate_Time = ptmptime_endall-ptmptime_start-m_GdalIOInCal_Time;

	double tmp=Mpi_Caculate_Time;
	MPI_Reduce(&Mpi_Caculate_Time,&tmp,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD);
	Mpi_Caculate_Time = tmp;
}