Beispiel #1
0
void
EllipseNode::rebuild()
{
    osgEarth::clearChildren( getPositionAttitudeTransform() );

    // construct a local-origin ellipse.
    GeometryFactory factory;
    Geometry* geom = NULL;

    if (std::abs(_arcEnd.as(Units::DEGREES) - _arcStart.as(Units::DEGREES)) >= 360.0)
    {
        geom = factory.createEllipse(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _numSegments);
    }
    else
    {
        geom = factory.createEllipticalArc(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _arcStart, _arcEnd, _numSegments, 0L, _pie);
    }
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<Feature> feature = new Feature(geom, 0L); //todo: consider the SRS
        osg::ref_ptr<osg::Node> node = compiler.compile( feature.get(), _style, FilterContext(0L) );
        if ( node )
        {
            node = AnnotationUtils::installOverlayParent(node.get(), _style);
            getPositionAttitudeTransform()->addChild( node.get() );
        }

        applyRenderSymbology( _style );
        setLightingIfNotSet( false );

    }
}
Beispiel #2
0
void
CircleNode::rebuild()
{
    std::string currentDecoration = getDecoration();
    clearDecoration();

    //Remove all children from this node
    //removeChildren( 0, getNumChildren() );
    if ( getRoot()->getNumParents() == 0 )
    {
        this->addChild( getRoot() );
    }

    //Remove all children from the attach point
    getChildAttachPoint()->removeChildren( 0, getChildAttachPoint()->getNumChildren() );

    // construct a local-origin circle.
    GeometryFactory factory;
    Geometry* geom = factory.createCircle(osg::Vec3d(0,0,0), _radius, _numSegments);
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<Feature> feature = new Feature(geom, 0L); //todo: consider the SRS
        osg::Node* node = compiler.compile( feature.get(), _style, FilterContext(0L) );
        if ( node )
        {           
            getChildAttachPoint()->addChild( node );
            getDrapeable()->setDraped( _draped );
        }

        applyStyle( _style );
    }

    setDecoration( currentDecoration );
}
Beispiel #3
0
void
RectangleNode::rebuild()
{    
    std::string currentDecoration = getDecoration();
    clearDecoration();

    // Reset:
    osgEarth::clearChildren( this );
    osgEarth::clearChildren( _xform.get() );
    this->addChild( _xform.get() );

    // construct a local-origin circle.
    GeometryFactory factory;    
    Geometry* geom = factory.createRectangle(osg::Vec3d(0,0,0), _width, _height);
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<Feature> feature = new Feature(geom, 0L); //todo: consider the SRS
        osg::Node* node = compiler.compile( feature.get(), _style, FilterContext(0L) );
        if ( node )
        {
            _xform->addChild( node );
            replaceChild( _xform.get(), applyAltitudePolicy(_xform.get(), _style) );
        }

        applyRenderSymbology( _style );
        setLightingIfNotSet( false );
    }

    setDecoration( currentDecoration );
}
/* private */
auto_ptr<GeometryFactory>
GeometryPrecisionReducer::createFactory( const GeometryFactory& oldGF,
                                         const PrecisionModel& newPM )
{
  auto_ptr<GeometryFactory> newFactory(
    new GeometryFactory(&newPM,
                        oldGF.getSRID(),
                        const_cast<CoordinateSequenceFactory*>(oldGF.getCoordinateSequenceFactory()))
  );
  return newFactory;
}
Beispiel #5
0
	Geometry* FeatureShp::CreateGeometry(long index, SHPHandle pSHPHandle)
	{
		if(pSHPHandle==NULL)
		{
			return NULL;
		}

		int	  iWKBLen = 0;
		g_uchar* pWKB = NULL;
		Geometry*	pGeometry  = NULL;
		SHPObject*	pSHPObject = NULL;
		GeometryFactory* pGeometryFactory = augeGetGeometryFactoryInstance();

		pSHPObject = ::SHPReadObject(pSHPHandle, index);
		if(pSHPObject==NULL)
		{
			return NULL;
		}

		iWKBLen = ShpUtil::GetWKBLength(pSHPObject);
		if(iWKBLen==0)
		{
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}

		pWKB = (g_uchar*)auge_malloc(iWKBLen,sizeof(g_uchar));
		if(pWKB==NULL)
		{
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}
		memset(pWKB, 0, iWKBLen);
		if(ShpUtil::SHPObject_2_WKB(pSHPObject, pWKB, iWKBLen)==0)
		{
			delete[] pWKB;
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}

		pGeometry = pGeometryFactory->CreateGeometryFromWKB(pWKB, true);

		if(pGeometry==NULL)
		{
			delete[] pWKB;
		}

		::SHPDestroyObject(pSHPObject);

		return pGeometry;
	}
Beispiel #6
0
void GeometryFactory::New(const FunctionCallbackInfo<Value>& args) {
    GeometryFactory* factory;
    if (args.Length() == 0) {
        factory = new GeometryFactory();
    } else if (args.Length() == 1) {
        PrecisionModel* model = ObjectWrap::Unwrap<PrecisionModel>(args[0]->ToObject());
        factory = new GeometryFactory(model->_model);
    } else {
        PrecisionModel* model = ObjectWrap::Unwrap<PrecisionModel>(args[0]->ToObject());
        int newSRID = args[1]->IntegerValue();
        factory = new GeometryFactory(model->_model, newSRID);
    }
    factory->Wrap(args.This());
    args.GetReturnValue().Set(args.This());
}
/* public */
GeometryPrecisionReducer::GeometryPrecisionReducer(const GeometryFactory &changeFactory)
      :
      newFactory(&changeFactory),
      targetPM(*(changeFactory.getPrecisionModel())),
      removeCollapsed(true),
      isPointwise(false)
{}
Beispiel #8
0
Handle<Value> GeometryFactory::New(const Arguments& args) {
    HandleScope scope;
    GeometryFactory* factory;
    if (args.Length() == 0) {
        factory = new GeometryFactory();
    } else if (args.Length() == 1) {
        PrecisionModel* model = ObjectWrap::Unwrap<PrecisionModel>(args[0]->ToObject());
        factory = new GeometryFactory(model->_model);
    } else {
        PrecisionModel* model = ObjectWrap::Unwrap<PrecisionModel>(args[0]->ToObject());
        int newSRID = args[1]->IntegerValue();
        factory = new GeometryFactory(model->_model, newSRID);
    }
    factory->Wrap(args.This());
    return args.This();
}
Beispiel #9
0
char *get_wkt_simple(osmNode *nodes, int count, int polygon) {
    GeometryFactory gf;
    std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));

    try
    {
        for (int i = 0; i < count ; i++) {
            Coordinate c;
            c.x = nodes[i].lon;
            c.y = nodes[i].lat;
            coords->add(c, 0);
        }

        geom_ptr geom;
        if (polygon && (coords->getSize() >= 4) && (coords->getAt(coords->getSize() - 1).equals2D(coords->getAt(0)))) {
            std::auto_ptr<LinearRing> shell(gf.createLinearRing(coords.release()));
            geom = geom_ptr(gf.createPolygon(shell.release(), new std::vector<Geometry *>));
            if (!geom->isValid()) {
                if (excludepoly) {
                    return NULL;
                } else {   
                    geom = geom_ptr(geom->buffer(0));
                }
            }
            geom->normalize(); // Fix direction of ring
        } else {
            if (coords->getSize() < 2)
                return NULL;
            geom = geom_ptr(gf.createLineString(coords.release()));
        }

        WKTWriter wktw;
        std::string wkt = wktw.write(geom.get());
        return strdup(wkt.c_str());
    }
    catch (std::bad_alloc)
    {
        std::cerr << std::endl << "Exception caught processing way. You are likelly running out of memory." << std::endl;
        std::cerr << "Try in slim mode, using -s parameter." << std::endl;
        return NULL;
    }
    catch (...)
    {
        std::cerr << std::endl << "Exception caught processing way" << std::endl;
        return NULL;
    }
}
Beispiel #10
0
/* public */
std::auto_ptr<LineString>
LineSegment::toGeometry(const GeometryFactory& gf) const
{
	CoordinateSequence *cl=new CoordinateArraySequence();
	cl->add(p0);
	cl->add(p1);
	return std::auto_ptr<LineString>(
		gf.createLineString(cl) // ownership transferred
	);
}
void
EllipseNode::rebuild()
{
    std::string currentDecoration = getDecoration();
    clearDecoration();

    //Remove all children from this node
    removeChildren( 0, getNumChildren() );

    //Remove all children from the attach point
    getAttachPoint()->removeChildren( 0, getAttachPoint()->getNumChildren() );

    // construct a local-origin ellipse.
    GeometryFactory factory;
    Geometry* geom = factory.createEllipse(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _numSegments);
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<Feature> feature = new Feature(geom, 0L); //todo: consider the SRS
        osg::Node* node = compiler.compile( feature.get(), _style, FilterContext(0L) );
        if ( node )
        {
            getAttachPoint()->addChild( node );

            if ( _draped )
            {
                DrapeableNode* drapeable = new DrapeableNode( _mapNode.get() );
                drapeable->addChild( getAttachPoint() );
                this->addChild( drapeable );
            }

            else
            {
                this->addChild( getAttachPoint() );
            }
        }

        applyStyle( _style, _draped );
    }

    setDecoration( currentDecoration );
}
void
EllipseNode::rebuildGeometry()
{
    // construct a local-origin ellipse.
    GeometryFactory factory;

    osg::ref_ptr<Geometry> geom;

    if (std::abs(_arcEnd.as(Units::DEGREES) - _arcStart.as(Units::DEGREES)) >= 360.0)
    {
        geom = factory.createEllipse(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _numSegments);
    }
    else
    {
        geom = factory.createEllipticalArc(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _arcStart, _arcEnd, _numSegments, 0L, _pie);
    }
    if ( geom.valid() )
    {
        setGeometry( geom.get() );
    }
}
Beispiel #13
0
void
EllipseNode::rebuild()
{
    std::string currentDecoration = getDecoration();
    clearDecoration();

    //Remove all children from this node
    osgEarth::clearChildren( this );
    osgEarth::clearChildren( _xform.get() );
    this->addChild( _xform.get() );

    // construct a local-origin ellipse.
    GeometryFactory factory;
    Geometry* geom = NULL;

    if (std::abs(_arcEnd.as(Units::DEGREES) - _arcStart.as(Units::DEGREES)) >= 360.0)
    {
        geom = factory.createEllipse(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _numSegments);
    }
    else
    {
        geom = factory.createEllipticalArc(osg::Vec3d(0,0,0), _radiusMajor, _radiusMinor, _rotationAngle, _arcStart, _arcEnd, _numSegments, 0L, _pie);
    }
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<Feature> feature = new Feature(geom, 0L); //todo: consider the SRS
        osg::Node* node = compiler.compile( feature.get(), _style, FilterContext(0L) );
        if ( node )
        {
            _xform->addChild( node );
            this->replaceChild( _xform.get(), applyAltitudePolicy(_xform.get(), _style) );
        }

        applyRenderSymbology( _style );
        setLightingIfNotSet( false );
    }

    setDecoration( currentDecoration );
}
void
RectangleNode::rebuild()
{    
    osgEarth::clearChildren( getPositionAttitudeTransform() );

    // construct a local-origin circle.
    GeometryFactory factory;    
    Geometry* geom = factory.createRectangle(osg::Vec3d(0,0,0), _width, _height);
    if ( geom )
    {
        GeometryCompiler compiler;
        osg::ref_ptr<osg::Node> node = compiler.compile( geom, _style, FilterContext() );
        if ( node )
        {
            node = AnnotationUtils::installOverlayParent( node.get(), _style );
            getPositionAttitudeTransform()->addChild( node.get() );
        }

        applyRenderSymbology( _style );
        setLightingIfNotSet( false );
    }
}
  // Push newly created geoms to rectLit
  void createRectangles(const Envelope& env, int nRect, double rectSize,
                        vector<const Geometry*>& rectList)
  {
    int nSide =  1 + (int)sqrt((double) nRect);
    double dx = env.getWidth() / nSide;
    double dy = env.getHeight() / nSide;

    for (int i = 0; i < nSide; i++) {
      for (int j = 0; j < nSide; j++) {
        double baseX = env.getMinX() + i * dx;
        double baseY = env.getMinY() + j * dy;
        Envelope envRect(
            baseX, baseX + dx,
            baseY, baseY + dy);
        Geometry* rect = fact.toGeometry(&envRect);
        rectList.push_back(rect);
      }
    }
  }
Beispiel #16
0
	GValue* TransactionHandler::CreateValue(XNode* pxNode, GField* pField)
	{
		GValue* pValue = NULL;
		const char* str= NULL;
		switch(pField->GetType())
		{					 
		case augeFieldTypeShort:
			{
				str = pxNode->GetContent();
				pValue = new GValue((short)atoi(str));
			}
			break;
		case augeFieldTypeInt:
			{
				str = pxNode->GetContent();
				pValue = new GValue((int)atoi(str));
			}
			break;
		case augeFieldTypeLong:
			{
				str = pxNode->GetContent();
				pValue = new GValue((long)atoi(str));
			}
			break;
		case augeFieldTypeInt64:
			{
				str = pxNode->GetContent();
				pValue = new GValue((int64)atoi(str));
			}
			break;
		case augeFieldTypeFloat:
			{
				str = pxNode->GetContent();
				pValue = new GValue((float)atof(str));
			}
			break;
		case augeFieldTypeDouble:
			{
				str = pxNode->GetContent();
				pValue = new GValue((double)atof(str));
			}
			break;
		case augeFieldTypeChar:			 
			{
				str = pxNode->GetContent();
				pValue = new GValue(str[0]);
			}
			break;
		case augeFieldTypeString:
			{
				pValue = new GValue(pxNode->GetContent());
			}
			break;
		case augeFieldTypeTime:	
			{
				str = pxNode->GetContent();
				if(str!=NULL)
				{
					TIME_STRU tim;
					memset(&tim,0, sizeof(TIME_STRU));
					sscanf(str,"%d-%2d-%2d %2d:%2d:%2d",&(tim.usYear),&(tim.usMonth),&(tim.usDay),&(tim.usHour),&(tim.usMinute),&(tim.usSecond));
					pValue = new GValue(&tim,true);
				}
			}
			break;
		case augeFieldTypeBool:			 
			{

			}
			break;
		case augeFieldTypeBLOB:			 
			{

			}
			break;
		case augeFieldTypeGeometry:
			{
				XNode* pxGeometry = pxNode->GetFirstChild();
				if(pxGeometry==NULL)
				{
					const char* msg = "Geometry Node is NULL";
					augeGetLoggerInstance()->Error(msg,__FILE__,__LINE__);		
				}
				else
				{
					Geometry* pGeometry = NULL;
					GeometryFactory* factory = augeGetGeometryFactoryInstance();
					GMLReader* reader = factory->CreateGMLReader();
					pGeometry = reader->Read(static_cast<XElement*>(pxGeometry));
					reader->Release();

					if(pGeometry==NULL)
					{
						const char* msg = "Invalid Geometry";
						augeGetLoggerInstance()->Error(msg,__FILE__,__LINE__);	
						const char* text = pxGeometry->ToString();
						augeGetLoggerInstance()->Error(text,__FILE__,__LINE__);	
					}
					else
					{
						pValue = new GValue(pGeometry);
					}	
				}
			}
			break;
		}

		return pValue;
	}
Beispiel #17
0
    void GeoWaveWriter::write(const PointViewPtr view)
    {

        using namespace Dimension;

        std::ostringstream os;

        BasicAccumuloOperations accumuloOperations;
        try
        {
            accumuloOperations = java_new<BasicAccumuloOperations>(
                java_new<String>(m_zookeeperUrl),
                java_new<String>(m_instanceName),
                java_new<String>(m_username),
                java_new<String>(m_password),
                java_new<String>(m_tableNamespace));
        }
        catch (AccumuloException& e)
        {
            log()->get(LogLevel::Error) << "There was a problem establishing a connector. " << e;
            return;
        }
        catch (AccumuloSecurityException& e)
        {
            log()->get(LogLevel::Error) << "The credentials passed are invalid. " << e;
            return;
        }

        AccumuloDataStore accumuloDataStore = java_new<AccumuloDataStore>(
            accumuloOperations);

        Index index = IndexType_JaceIndexType::createSpatialVectorIndex();

        AccumuloIndexWriter accumuloIndexWriter = java_new<AccumuloIndexWriter>(
            index,
            accumuloOperations,
            accumuloDataStore);

        // treat all types as double
        os << "location:Point:srid=4326";
        for (auto di = m_dims.begin(); di != m_dims.end(); ++di)
            os << "," << view->dimName(*di) << ":Double";

        SimpleFeatureType TYPE = DataUtilities::createType(
            java_new<String>(m_featureTypeName),
            java_new<String>(os.str()));

        String location = java_new<String>("location");

        WritableDataAdapter dataAdapter;
        if (m_useFeatCollDataAdapter)
            dataAdapter = java_new<FeatureCollectionDataAdapter>(
            TYPE,
            m_pointsPerEntry);
        else
            dataAdapter = java_new<FeatureDataAdapter>(TYPE);


        GeometryFactory geometryFactory = JTSFactoryFinder::getGeometryFactory();
        SimpleFeatureBuilder builder = java_new<SimpleFeatureBuilder>(TYPE);

        DefaultFeatureCollection featureCollection = java_new<DefaultFeatureCollection>(
            UUID::randomUUID().toString(),
            TYPE);

        for (PointId idx = 0; idx < view->size(); ++idx)
        {
            JDouble X = view->getFieldAs<double>(Id::X, idx);
            JDouble Y = view->getFieldAs<double>(Id::Y, idx);

            Point point = geometryFactory.createPoint(
                java_new<Coordinate>(
                X,
                Y));

            builder.set(location, point);

            for (auto di = m_dims.begin(); di != m_dims.end(); ++di)
                if (view->hasDim(*di))
                    builder.set(java_new<String>(view->dimName(*di)), java_new<Double>(view->getFieldAs<double>(*di, idx)));

            SimpleFeature feature = builder.buildFeature(UUID::randomUUID().toString());

            if (m_useFeatCollDataAdapter)
                featureCollection.add(feature);
            else
                accumuloIndexWriter.write(
                dataAdapter,
                feature);
        }

        if (m_useFeatCollDataAdapter)
            accumuloIndexWriter.write(
            dataAdapter,
            featureCollection);

        accumuloIndexWriter.close();
    }
	void PolygonToLineProcessorImpl::ProcessMultiPolygon(Feature* pinFeature, FeatureClass* poutFeatureClass, FeatureInsertCommand* cmd)
	{
		Geometry* pGeometry = NULL;
		pGeometry = pinFeature->GetGeometry();
		if(pGeometry==NULL)
		{
			return;
		}

		const char* fname = NULL;
		GField*  pField = NULL;
		GFields* pFields = pinFeature->GetFeatureClass()->GetFields();
		g_uint count = pFields->Count();

		GValue* pValue = NULL;
		GValue* pGeoValue = NULL;		
		Feature* poutFeature = poutFeatureClass->NewFeature();

		augeFieldType type = augeFieldTypeNone;
		for(g_uint i=0; i<count; i++)
		{
			pField = pFields->GetField(i);
			type = pField->GetType();
			if(type==augeFieldTypeGeometry)
			{
				continue;
			}

			fname = pField->GetName();
			pValue = pinFeature->GetValue(i);			
			poutFeature->SetValue(fname, pValue);
		}

		Geometry* pGeoLine = NULL;
		GeometryFactory* pGeometryFactory = augeGetGeometryFactoryInstance();
		pField = pinFeature->GetFeatureClass()->GetFields()->GetGeometryField();
		if(pField!=NULL)
		{
			pValue = pinFeature->GetValue(pField->GetName());
			if(pValue!=NULL)
			{
				pGeometry = pValue->GetGeometry();
				if(pGeometry!=NULL)
				{
					g_uint numPoints = 0;
					g_uint numRings = 0;
					g_uint numPolygons = 0;
					g_uint ring_size = 0;
					g_uint line_size = 0;
					LinearRing* pLinearRing = NULL;
					WKBPolygon* pWKBPolygon = NULL;
					WKBMultiPolygon* pWKBMultiPolygon = (WKBMultiPolygon*)pGeometry->AsBinary();
					WKBLineString* pWKBLineString = NULL;
					g_byte* ptr = NULL;

					numPolygons = pWKBMultiPolygon->numPolygons;
					pWKBPolygon = (WKBPolygon*)(&(pWKBMultiPolygon->polygons[0]));
					for(g_uint i=0; i<numPolygons; i++)
					{
						numRings = pWKBPolygon->numRings;
						pLinearRing = (LinearRing*)(&(pWKBPolygon->rings[0]));
						for(g_uint j=0; j<numRings; j++)
						{
							numPoints = pLinearRing->numPoints;
							ring_size = sizeof(auge::Point) * numPoints + sizeof(g_int32);
							line_size = ring_size + sizeof(g_int32) + sizeof(g_byte);

							pWKBLineString = (WKBLineString*)malloc(line_size);
							memset(pWKBLineString, 0, line_size);

							pWKBLineString->byteOrder = coDefaultByteOrder;
							pWKBLineString->wkbType = wkbLineString;
							pWKBLineString->numPoints = numPoints;
							memcpy(&(pWKBLineString->points[0]), &(pLinearRing->points[0]), sizeof(auge::Point) * numPoints);

							pGeoLine = pGeometryFactory->CreateGeometryFromWKB((g_byte*)(pWKBLineString), true);
							pGeoValue = new GValue(pGeoLine);
							poutFeature->SetValue(pField->GetName(), pGeoValue);

							cmd->Insert(poutFeature);

							free(pWKBLineString);

							ptr = (g_byte*)pLinearRing;
							pLinearRing = (LinearRing*)(ptr + ring_size);
						}
						pWKBPolygon = (WKBPolygon*)pLinearRing;
					}
				}
			}
		}
		poutFeature->Release();
	}
Beispiel #19
0
size_t build_geometry(osmid_t osm_id, struct osmNode **xnodes, int *xcount, int make_polygon, int enable_multi, double split_at) {
    size_t wkt_size = 0;
    std::auto_ptr<std::vector<Geometry*> > lines(new std::vector<Geometry*>);
    GeometryFactory gf;
    geom_ptr geom;
#ifdef HAS_PREPARED_GEOMETRIES
    geos::geom::prep::PreparedGeometryFactory pgf;
#endif

    try
    {
        for (int c=0; xnodes[c]; c++) {
            std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
            for (int i = 0; i < xcount[c]; i++) {
                struct osmNode *nodes = xnodes[c];
                Coordinate c;
                c.x = nodes[i].lon;
                c.y = nodes[i].lat;
                coords->add(c, 0);
            }
            if (coords->getSize() > 1) {
                geom = geom_ptr(gf.createLineString(coords.release()));
                lines->push_back(geom.release());
            }
        }

        //geom_ptr segment(0);
        geom_ptr mline (gf.createMultiLineString(lines.release()));
        //geom_ptr noded (segment->Union(mline.get()));
        LineMerger merger;
        //merger.add(noded.get());
        merger.add(mline.get());
        std::auto_ptr<std::vector<LineString *> > merged(merger.getMergedLineStrings());
        WKTWriter writer;

        // Procces ways into lines or simple polygon list
        polygondata* polys = new polygondata[merged->size()];

        unsigned totalpolys = 0;
        for (unsigned i=0 ;i < merged->size(); ++i)
        {
            std::auto_ptr<LineString> pline ((*merged ) [i]);
            if (make_polygon && pline->getNumPoints() > 3 && pline->isClosed())
            {
                polys[totalpolys].polygon = gf.createPolygon(gf.createLinearRing(pline->getCoordinates()),0);
                polys[totalpolys].ring = gf.createLinearRing(pline->getCoordinates());
                polys[totalpolys].area = polys[totalpolys].polygon->getArea();
                polys[totalpolys].iscontained = 0;
		polys[totalpolys].containedbyid = 0;
                if (polys[totalpolys].area > 0.0)
                    totalpolys++;
                else {
                    delete(polys[totalpolys].polygon);
                    delete(polys[totalpolys].ring);
                }
            }
            else
            {
                        //std::cerr << "polygon(" << osm_id << ") is no good: points(" << pline->getNumPoints() << "), closed(" << pline->isClosed() << "). " << writer.write(pline.get()) << std::endl;
                double distance = 0;
                std::auto_ptr<CoordinateSequence> segment;
                segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
                segment->add(pline->getCoordinateN(0));
                for(unsigned i=1; i<pline->getNumPoints(); i++) {
                    segment->add(pline->getCoordinateN(i));
                    distance += pline->getCoordinateN(i).distance(pline->getCoordinateN(i-1));
                    if ((distance >= split_at) || (i == pline->getNumPoints()-1)) {
                        geom = geom_ptr(gf.createLineString(segment.release()));
                        std::string wkt = writer.write(geom.get());
                        wkts.push_back(wkt);
                        areas.push_back(0);
                        wkt_size++;
                        distance=0;
                        segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
                        segment->add(pline->getCoordinateN(i));
                    }
                }
                //std::string text = writer.write(pline.get());
                //wkts.push_back(text);
                //areas.push_back(0.0);
                //wkt_size++;
            }
        }

        if (totalpolys)
        {
            qsort(polys, totalpolys, sizeof(polygondata), polygondata_comparearea);

            unsigned toplevelpolygons = 0;
            int istoplevelafterall;

            for (unsigned i=0 ;i < totalpolys; ++i)
            {
                if (polys[i].iscontained != 0) continue;
                toplevelpolygons++;
#ifdef HAS_PREPARED_GEOMETRIES
                const geos::geom::prep::PreparedGeometry* preparedtoplevelpolygon = pgf.create(polys[i].polygon);
#endif

                for (unsigned j=i+1; j < totalpolys; ++j)
                {
#ifdef HAS_PREPARED_GEOMETRIES
                    // Does preparedtoplevelpolygon contain the smaller polygon[j]?
                    if (polys[j].containedbyid == 0 && preparedtoplevelpolygon->contains(polys[j].polygon))
#else
                    // Does polygon[i] contain the smaller polygon[j]?
                    if (polys[j].containedbyid == 0 && polys[i].polygon->contains(polys[j].polygon))
#endif
                    {
                        // are we in a [i] contains [k] contains [j] situation
                        // which would actually make j top level
                        istoplevelafterall = 0;
                        for (unsigned k=i+1; k < j; ++k)
                        {
                            if (polys[k].iscontained && polys[k].containedbyid == i && polys[k].polygon->contains(polys[j].polygon))
                            {
                                istoplevelafterall = 1;
                                break;
                            }
#if 0
                            else if (polys[k].polygon->intersects(polys[j].polygon) || polys[k].polygon->touches(polys[j].polygon))
			    {
                                // FIXME: This code does not work as intended
                                // It should be setting the polys[k].ring in order to update this object
                                // but the value of polys[k].polygon calculated is normally NULL

                                // Add polygon this polygon (j) to k since they intersect
				// Mark ourselfs to be dropped (2), delete the original k
                                Geometry* polyunion = polys[k].polygon->Union(polys[j].polygon);
                                delete(polys[k].polygon);
				polys[k].polygon = dynamic_cast<Polygon*>(polyunion); 
				polys[j].iscontained = 2; // Drop
                                istoplevelafterall = 2;
                                break;

                            }
#endif
                        }
                        if (istoplevelafterall == 0)
                        {
                            polys[j].iscontained = 1;
                            polys[j].containedbyid = i;
                        }
                    }
                }
#ifdef HAS_PREPARED_GEOMETRIES
		pgf.destroy(preparedtoplevelpolygon);
#endif
            }
            // polys now is a list of ploygons tagged with which ones are inside each other

            // List of polygons for multipolygon
            std::auto_ptr<std::vector<Geometry*> > polygons(new std::vector<Geometry*>);

            // For each top level polygon create a new polygon including any holes
            for (unsigned i=0 ;i < totalpolys; ++i)
            {
                if (polys[i].iscontained != 0) continue;

                // List of holes for this top level polygon
                std::auto_ptr<std::vector<Geometry*> > interior(new std::vector<Geometry*>);
                for (unsigned j=i+1; j < totalpolys; ++j)
                {
                   if (polys[j].iscontained == 1 && polys[j].containedbyid == i)
                   {
                       interior->push_back(polys[j].ring);
                   }
                }
                
                Polygon* poly(gf.createPolygon(polys[i].ring, interior.release()));
                poly->normalize();
                polygons->push_back(poly);
            }

            // Make a multipolygon if required
            if ((toplevelpolygons > 1) && enable_multi)
            {
                std::auto_ptr<MultiPolygon> multipoly(gf.createMultiPolygon(polygons.release()));
                //if (multipoly->isValid())
                //{
                    std::string text = writer.write(multipoly.get());
                    wkts.push_back(text);
                    areas.push_back(multipoly->getArea());
                    wkt_size++;
                //}
            }
            else
            {
                for(unsigned i=0; i<toplevelpolygons; i++) 
                {
                    Polygon* poly = dynamic_cast<Polygon*>(polygons->at(i));;
                    //if (poly->isValid())
                    //{
                        std::string text = writer.write(poly);
                        wkts.push_back(text);
                        areas.push_back(poly->getArea());
                        wkt_size++;
                    //}
                    delete(poly);
                }
            }
        }

        for (unsigned i=0; i < totalpolys; ++i)
        {
            delete(polys[i].polygon);
        }
        delete[](polys);
    }
    catch (std::exception& e)
      {
	std::cerr << std::endl << "Standard exception processing way_id "<< osm_id << ": " << e.what()  << std::endl;
	wkt_size = 0;
      }
    catch (...)
      {
        std::cerr << std::endl << "Exception caught processing way id=" << osm_id << std::endl;
        wkt_size = 0;
      }

    return wkt_size;
}
Beispiel #20
0
size_t get_wkt_split(osmNode *nodes, int count, int polygon, double split_at) {
    GeometryFactory gf;
    std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
    double area;
    WKTWriter wktw;
    size_t wkt_size = 0;

    try
    {
        for (int i = 0; i < count ; i++) {
            Coordinate c;
            c.x = nodes[i].lon;
            c.y = nodes[i].lat;
            coords->add(c, 0);
        }

        geom_ptr geom;
        if (polygon && (coords->getSize() >= 4) && (coords->getAt(coords->getSize() - 1).equals2D(coords->getAt(0)))) {
            std::auto_ptr<LinearRing> shell(gf.createLinearRing(coords.release()));
            geom = geom_ptr(gf.createPolygon(shell.release(), new std::vector<Geometry *>));
            geom->normalize(); // Fix direction of ring
            area = geom->getArea();
            std::string wkt = wktw.write(geom.get());
            wkts.push_back(wkt);
            areas.push_back(area);
            wkt_size++;
        } else {
            if (coords->getSize() < 2)
                return 0;

            double distance = 0;
            std::auto_ptr<CoordinateSequence> segment;
            segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
            segment->add(coords->getAt(0));
            for(unsigned i=1; i<coords->getSize(); i++) {
                segment->add(coords->getAt(i));
                distance += coords->getAt(i).distance(coords->getAt(i-1));
                if ((distance >= split_at) || (i == coords->getSize()-1)) {
                    geom = geom_ptr(gf.createLineString(segment.release()));
                    std::string wkt = wktw.write(geom.get());
                    wkts.push_back(wkt);
                    areas.push_back(0);
                    wkt_size++;
                    distance=0;
                    segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
                    segment->add(coords->getAt(i));
                }
            }
        }

    }
    catch (std::bad_alloc)
    {
        std::cerr << std::endl << "Exception caught processing way. You are likelly running out of memory." << std::endl;
        std::cerr << "Try in slim mode, using -s parameter." << std::endl;
        wkt_size = 0;
    }
    catch (...)
    {
        std::cerr << std::endl << "Exception caught processing way" << std::endl;
        wkt_size = 0;
    }
    return wkt_size;
}