Пример #1
0
        /************************************************************************/
        /*                           SHPCreateTree()                            */
        /************************************************************************/
        public static SHPTree Create(SHPHandle hSHP, int nDimension, int nMaxDepth,
                                     double[] padfBoundsMin, double[] padfBoundsMax)

        {
            SHPTree psTree;

            if (padfBoundsMin == null && hSHP == null)
            {
                return(null);
            }

            /* -------------------------------------------------------------------- */
            /*      Allocate the tree object                                        */
            /* -------------------------------------------------------------------- */
            psTree = new SHPTree();

            psTree.hSHP       = hSHP;
            psTree.nMaxDepth  = nMaxDepth;
            psTree.nDimension = nDimension;

            /* -------------------------------------------------------------------- */
            /*      If no max depth was defined, try to select a reasonable one     */
            /*      that implies approximately 8 shapes per node.                   */
            /* -------------------------------------------------------------------- */
            if (psTree.nMaxDepth == 0 && hSHP != null)
            {
                int  nMaxNodeCount = 1;
                int  nShapeCount;
                SHPT nShapeType;

                hSHP.GetInfo(out nShapeCount, out nShapeType, null, null);
                while (nMaxNodeCount * 4 < nShapeCount)
                {
                    psTree.nMaxDepth += 1;
                    nMaxNodeCount     = nMaxNodeCount * 2;
                }
            }

            /* -------------------------------------------------------------------- */
            /*      Allocate the root node.                                         */
            /* -------------------------------------------------------------------- */
            psTree.psRoot = NodeCreate(padfBoundsMin, padfBoundsMax);

            /* -------------------------------------------------------------------- */
            /*      Assign the bounds to the root node.  If none are passed in,     */
            /*      use the bounds of the provided file otherwise the create        */
            /*      function will have already set the bounds.                      */
            /* -------------------------------------------------------------------- */
            if (padfBoundsMin == null)
            {
                int  nShapeCount;
                SHPT nShapeType;

                hSHP.GetInfo(out nShapeCount, out nShapeType,
                             psTree.psRoot.adfBoundsMin,
                             psTree.psRoot.adfBoundsMax);
            }

            /* -------------------------------------------------------------------- */
            /*      If we have a file, insert all it's shapes into the tree.        */
            /* -------------------------------------------------------------------- */
            if (hSHP != null)
            {
                int  iShape, nShapeCount;
                SHPT nShapeType;

                hSHP.GetInfo(out nShapeCount, out nShapeType, null, null);

                for (iShape = 0; iShape < nShapeCount; iShape++)
                {
                    SHPObject psShape;

                    psShape = hSHP.ReadObject(iShape);
                    psTree.AddShapeId(psShape);
                    psShape = null;
                }
            }

            return(psTree);
        }