예제 #1
0
 public ArchiveParser(byte[] archive)
 {
     this.data = archive;
     archiveReader = new BigEndianBinaryReader(new MemoryStream(this.data));
     DecompressedSize = archiveReader.ReadUInt24();
     CompressedSize = archiveReader.ReadUInt24();
     if (CompressedSize != DecompressedSize)
     {
         byte[] input = new byte[DecompressedSize - 6];
         Array.Copy(this.data, 6, input, 0, DecompressedSize - 6);
         byte[] dec = new byte[DecompressedSize - 6];
         BZip2.Decompress(new MemoryStream(input), new MemoryStream(dec), true);
         finalBuffer = dec;
         archiveReader.Close();
         archiveReader = new BigEndianBinaryReader(new MemoryStream(finalBuffer));
         compressedAsWhole = true;
     }
     else
     {
         finalBuffer = this.data;
         compressedAsWhole = false;
     }
     totalFiles = archiveReader.ReadUInt16();
     int offset = 8 + totalFiles * 10;
     for (int i = 0; i < totalFiles; i++)
     {
         identifiers.Add(archiveReader.ReadInt32());
         decompressedSizes.Add(archiveReader.ReadUInt24());
         compressedSizes.Add(archiveReader.ReadUInt24());
         startOffsets.Add(offset);
         Logger.Log("Found file: " + identifiers[i] + " at " + offset + ", size: " + compressedSizes[i], LogType.Success);
         offset += compressedSizes[i];
         files.Add(getFileAt(i));
     }
 }
예제 #2
0
		/// <summary>
		/// Initializes a new instance of the ShapefileHeader class with values read in from the stream.
		/// </summary>
		/// <remarks>Reads the header information from the stream.</remarks>
		/// <param name="shpBinaryReader">BigEndianBinaryReader stream to the shapefile.</param>
		public ShapefileHeader(BigEndianBinaryReader shpBinaryReader)
		{
			if (shpBinaryReader == null)
				throw new ArgumentNullException("shpBinaryReader");

			_fileCode = shpBinaryReader.ReadInt32BE();	
			if (_fileCode != Shapefile.ShapefileId)
				throw new ShapefileException("The first four bytes of this file indicate this is not a shape file.");

			// skip 5 unsed bytes
			shpBinaryReader.ReadInt32BE();
			shpBinaryReader.ReadInt32BE();
			shpBinaryReader.ReadInt32BE();
			shpBinaryReader.ReadInt32BE();
			shpBinaryReader.ReadInt32BE();

			_fileLength = shpBinaryReader.ReadInt32BE();

			_version = shpBinaryReader.ReadInt32();
			Debug.Assert(_version == 1000, "Shapefile version", String.Format("Expecting only one version (1000), but got {0}",_version));
			int shapeType = shpBinaryReader.ReadInt32();
            _shapeType = (ShapeGeometryType) Enum.Parse(typeof(ShapeGeometryType), shapeType.ToString());

			//read in and store the bounding box
			double[] coords = new double[4];
			for (int i = 0; i < 4; i++)
				coords[i] = shpBinaryReader.ReadDouble();
			_bounds = new Envelope(coords[0], coords[2], coords[1], coords[3]);
			
			// skip z and m bounding boxes.
			for (int i = 0; i < 4; i++)
				shpBinaryReader.ReadDouble();	
		}
예제 #3
0
 /// <summary>
 /// Runescape Font. Doesn't work!
 /// </summary>
 public RSFont(bool flag, string fontName, ArchiveParser titleArchive)
 {
     glyphPixels = new List<byte[]>();
     glyphWidth = new int[256];
     glyphHeight = new int[256];
     horizontalKerning = new int[256];
     verticalKerning = new int[256];
     charEffectiveWidth = new int[256];
     random = new Random();
     isStrikethrough = false;
     byte[] data = titleArchive.getFile(fontName + ".dat");
     byte[] index = titleArchive.getFile("index.dat");
     dataReader = new BigEndianBinaryReader(new MemoryStream(data));
     indexReader = new BigEndianBinaryReader(new MemoryStream(index));
     int pos = dataReader.ReadInt16() + 4;
     int k = indexReader.ReadByte();
     if (k > 0)
         pos += 3 * (k - 1);
     indexReader.BaseStream.Position = pos;
     for (int l = 0; l < 256; l++)
     {
         horizontalKerning[l] = indexReader.ReadChar();
         verticalKerning[l] = indexReader.ReadChar();
         int width = glyphWidth[l] = indexReader.ReadInt16();
         int height = glyphWidth[l] = indexReader.ReadInt16();
         int k1 = indexReader.ReadByte();
         int l1 = width * height;
         glyphPixels.Add(new byte[l1]);
         if (k1 == 0)
             for (int i = 0; i < l1; i++)
                 glyphPixels[l][i] = dataReader.ReadByte();
         else
             if (k1 == 1)
                 for (int j = 0; j < width; j++)
                     for (int l2 = 0; l2 < height; l2++)
                         glyphPixels[l][j + l2 * width] = dataReader.ReadByte();
         if (height > charHeight && l < 128)
             charHeight = height;
         horizontalKerning[l] = 1;
         charEffectiveWidth[l] = width + 2;
         int k2 = 0;
         for (int i = height / 7; i < height; i++)
             k2 += glyphPixels[l][i * width];
         if (k2 <= height / 7)
         {
             charEffectiveWidth[l]--;
             horizontalKerning[l] = 0;
         }
         k2 = 0;
         for (int j = height / 7; j < height; j++)
             k2 += glyphPixels[l][(width - 1) + j * width];
         if (k2 <= height / 7)
             charEffectiveWidth[l]--;
     }
     if (flag)
         charEffectiveWidth[32] = charEffectiveWidth[73];
     else
         charEffectiveWidth[32] = charEffectiveWidth[105];
 }
예제 #4
0
 public void ReadCache(int cacheNum)
 {
     if (!File.Exists(CacheDir + "main_file_cache.idx" + cacheNum))
         Logger.Log("Couldn't find index file #" + cacheNum + " in " + CacheDir, LogType.Fatal);
     cacheIndexStreams[cacheNum] = File.Open(CacheDir + "main_file_cache.idx" + cacheNum, FileMode.Open);
     cacheIndexReaders[cacheNum] = new BigEndianBinaryReader(cacheIndexStreams[cacheNum]);
     Logger.Log("Reading indice #" + cacheNum);
     cacheIndices.Add(new CacheIndice(ref cacheReader, ref cacheIndexReaders[cacheNum], cacheNum + 1));
     Logger.Log("Read indice #" + cacheNum, LogType.Success);
 }
예제 #5
0
		/// <summary>
		/// Reads a stream and converts the shapefile record to an equilivent geometry object.
		/// </summary>
		/// <param name="file">The stream to read.</param>
		/// <param name="geometryFactory">The geometry factory to use when making the object.</param>
		/// <returns>The Geometry object that represents the shape file record.</returns>
		public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory)
		{
			int shapeTypeNum = file.ReadInt32();
            ShapeGeometryTypes shapeType = (ShapeGeometryTypes) Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString());
            if ( ! ( shapeType == ShapeGeometryTypes.Point  || shapeType == ShapeGeometryTypes.PointM   ||
                     shapeType == ShapeGeometryTypes.PointZ || shapeType == ShapeGeometryTypes.PointZM  ))	
				throw new ShapefileException("Attempting to load a point as point.");
			double x= file.ReadDouble();
			double y= file.ReadDouble();
			ICoordinate external = new Coordinate(x,y);			
			geometryFactory.PrecisionModel.MakePrecise( external);
            return geometryFactory.CreatePoint(external);
		}
예제 #6
0
 public CacheIndice(ref BigEndianBinaryReader dataFile, ref BigEndianBinaryReader indexFile, int cacheNum)
 {
     maxFileSize = 6500000;
     this.cacheNum = cacheNum;
     this.dataReader = dataFile;
     this.indexReader = indexFile;
     for (int i = 0; i < indexFile.BaseStream.Length / 6; i++)
     {
         Logger.Log("Getting file: " + i, LogType.Message);
         byte[] data = getFile(i);
         if(data != null)
             Logger.Log("Found file: " + i + ", Length: " + data.Length + " bytes in cache "+cacheNum, LogType.Success);
         files.Add(data);
     }
 }
예제 #7
0
 //Parses flo.dat from config.jag
 //Yup it's fixed
 public static FloorConfig[] ParseFloorConfig(byte[] floorData)
 {
     int floorCount;
     FloorConfig[] floors;
     BigEndianBinaryReader floorReader = new BigEndianBinaryReader(new MemoryStream(floorData));
     floorCount = floorReader.ReadUInt16();
     floors = new FloorConfig[floorCount];
     bool finishFloor = false;
     for (int i = 0; i < floorCount; i++)
     {
         floors[i] = new FloorConfig();
         finishFloor = false;
         do
         {
             int type = floorReader.ReadByte();
             switch (type)
             {
                 case 0:
                     finishFloor = true;
                     break;
                 case 1:
                     floors[i].actualColor = toRGB(floorReader.ReadUInt24());
                     break;
                 case 2:
                     floors[i].texture = floorReader.ReadByte();
                     break;
                 case 3:
                     floors[i].unknown = true;
                     break;
                 case 5:
                     floors[i].occlude = false;
                     break;
                 case 6:
                     floors[i].floorName = floorReader.ReadString().TrimEnd('\n');
                     break;
                 case 7:
                     floors[i].mapColor = toRGB(floorReader.ReadUInt24());
                     break;
                 default:
                     Logger.Log("Unrecognized floor type: " + type, LogType.Error);
                     break;
             }
         } while (!finishFloor);
         Logger.Log(floors[i].ToString(), LogType.Success);
     }
     return floors;
 }
예제 #8
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ShapefileEnumerator"/> class.
            /// </summary>
            /// <param name="shapefile"></param>
            public ShapefileEnumerator(ShapefileReader shapefile)
            {                
                _parent = shapefile;

                // create a file stream for each enumerator that is given out. This allows the same file
                // to have one or more enumerator. If we used the parents stream - than only one IEnumerator 
                // could be given out.
                FileStream stream = new FileStream(_parent._filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                _shpBinaryReader = new BigEndianBinaryReader(stream);

                // skip header - since parent has already read this.
                _shpBinaryReader.ReadBytes(100);
                ShapeGeometryType type = _parent._mainHeader.ShapeType;
                _handler = Shapefile.GetShapeHandler(type);
                if (_handler == null) 
                    throw new NotSupportedException("Unsuported shape type:" + type);
            }
예제 #9
0
 public CacheReader(int cacheNum=-1)
 {
     if (!File.Exists(CacheDir+"main_file_cache.dat"))
         Logger.Log("Couldn't find cache file: "+CacheDir+"main_file_cache.dat", LogType.Fatal);
     cacheStream = File.Open(CacheDir + "main_file_cache.dat", FileMode.Open);
     cacheReader = new BigEndianBinaryReader(cacheStream);
     uid = (int)(new Random().NextDouble() * 99999999D);
     cacheIndexStreams = new FileStream[5];
     cacheIndexReaders = new BigEndianBinaryReader[5];
     cacheIndices = new List<CacheIndice>();
     if (cacheNum == -1)
         for (int i = 0; i < 5; i++)
             ReadCache(i);
     else if (cacheNum > -1 && cacheNum < 5)
         ReadCache(cacheNum);
     else
         throw new Exception("Invalid cache num!");
 }
예제 #10
0
		/// <summary>
		/// Reads a stream and converts the shapefile record to an equilivent geometry object.
		/// </summary>
		/// <param name="file">The stream to read.</param>
		/// <param name="geometryFactory">The geometry factory to use when making the object.</param>
		/// <returns>The Geometry object that represents the shape file record.</returns>
		public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory)
		{
			int shapeTypeNum = file.ReadInt32();
            ShapeGeometryTypes shapeType = (ShapeGeometryTypes)Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString());
            if( ! ( shapeType == ShapeGeometryTypes.LineString  || shapeType == ShapeGeometryTypes.LineStringM   ||
                    shapeType == ShapeGeometryTypes.LineStringZ || shapeType == ShapeGeometryTypes.LineStringZM  ))
				throw new ShapefileException("Attempting to load a non-arc as arc.");

			//read and for now ignore bounds.
			double[] box = new double[4];
			for (int i = 0; i < 4; i++) 
			{
				double d= file.ReadDouble();
				box[i] =d;
			}
        
			int numParts = file.ReadInt32();
			int numPoints = file.ReadInt32();
			int[] partOffsets = new int[numParts];
			for (int i = 0; i < numParts; i++)
				partOffsets[i] = file.ReadInt32();
			
			ILineString[] lines = new ILineString[numParts];
			int start, finish, length;
			for (int part = 0; part < numParts; part++)
			{
				start = partOffsets[part];
				if (part == numParts - 1)
					 finish = numPoints;
				else finish = partOffsets[part + 1];
				length = finish - start;
                CoordinateList points = new CoordinateList();
				points.Capacity=length;
				ICoordinate external;
				for (int i = 0; i < length; i++)
				{
					external = new Coordinate(file.ReadDouble(),file.ReadDouble());
					geometryFactory.PrecisionModel.MakePrecise( external);
                    points.Add(external);
				}
                lines[part] = geometryFactory.CreateLineString(points.ToArray());
			}
			return geometryFactory.CreateMultiLineString(lines);
		}
예제 #11
0
		/// <summary>
		/// Reads a stream and converts the shapefile record to an equilivant geometry object.
		/// </summary>
		/// <param name="file">The stream to read.</param>
		/// <param name="geometryFactory">The geometry factory to use when making the object.</param>
		/// <returns>The Geometry object that represents the shape file record.</returns>
        public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory)
        {
            int shapeTypeNum = file.ReadInt32();
            ShapeGeometryTypes shapeType = (ShapeGeometryTypes) Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString());
            if ( ! ( shapeType == ShapeGeometryTypes.MultiPoint  || shapeType == ShapeGeometryTypes.MultiPointM ||
                     shapeType == ShapeGeometryTypes.MultiPointZ || shapeType == ShapeGeometryTypes.MultiPointZM))	
                throw new ShapefileException("Attempting to load a non-multipoint as multipoint.");

            // Read and for now ignore bounds.
            double[] box = new double[4];
            for (int i = 0; i < 4; i++)
                box[i] = file.ReadDouble();

            // Read points
            int numPoints = file.ReadInt32();
            IPoint[] points = new IPoint[numPoints];
            for (int i = 0; i < numPoints; i++)
                points[i] = geometryFactory.CreatePoint(new Coordinate(file.ReadDouble(), file.ReadDouble()));
            return geometryFactory.CreateMultiPoint(points);
        }
예제 #12
0
        /// <summary>
        /// Reads Polygon shapefile
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected IList ReadPolygonData(Stream stream)
        {
            IList list = new ArrayList();

            // Jump to first header
            stream.Seek(100, SeekOrigin.Begin);

            // Read big endian informations
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Read little endian informations
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    // For each header
                    while (stream.Position < stream.Length)
                    {
                        ReadFeatureHeader(beReader);
                        shapeReader.ReadBoundingBox(reader);

                        int[] indexParts = null;
                        int   numParts   = shapeReader.ReadNumParts(reader);
                        int   numPoints  = shapeReader.ReadNumPoints(reader);

                        indexParts = shapeReader.ReadIndexParts(reader, numParts);
                        ICoordinate[] coordinates = shapeReader.ReadCoordinates(reader, numPoints);

                        if (numParts == 1)
                        {
                            list.Add(shapeReader.CreateSimpleSinglePolygon(coordinates));
                        }
                        else
                        {
                            list.Add(shapeReader.CreateSingleOrMultiPolygon(numPoints, indexParts, coordinates));
                        }
                    }
                }
            }
            return(list);
        }
예제 #13
0
        /// <summary>
        /// Initializes a new instance of the Shapefile class with the given parameters.
        /// </summary>
        /// <param name="filename">The filename of the shape file to read (with .shp).</param>
        /// <param name="geometryFactory">The GeometryFactory to use when creating Geometry objects.</param>
        public ShapefileReader(string filename, IGeometryFactory geometryFactory)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            if (geometryFactory == null)
            {
                throw new ArgumentNullException("geometryFactory");
            }

            _filename        = filename;
            _geometryFactory = geometryFactory;

            // read header information. note, we open the file, read the header information and then
            // close the file. This means the file is not opened again until GetEnumerator() is requested.
            // For each call to GetEnumerator() a new BinaryReader is created.
            FileStream            stream          = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            BigEndianBinaryReader shpBinaryReader = new BigEndianBinaryReader(stream);

            _mainHeader = new ShapefileHeader(shpBinaryReader);
            shpBinaryReader.Close();
        }
예제 #14
0
        /// <summary>
        /// Reads Point shapefile
        /// </summary>
        /// <param name="stream"></param>
        protected IList ReadPointData(Stream stream)
        {
            IList list = new ArrayList();

            // Jump to first header                 
            stream.Seek(100, SeekOrigin.Begin);

            // Read big endian informations
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Read little endian informations
                using (BinaryReader leReader = new BinaryReader(stream))
                {                                                                
                    // For each header                
                    while (stream.Position < stream.Length)
                    {
                        ReadFeatureHeader(beReader);                  
                                            
                        ICoordinate coordinate = shapeReader.ReadCoordinate(leReader);
                        IGeometry point = shapeReader.CreatePoint(coordinate);
                        list.Add(point);
                    }
                }              
            }
            return list;  
        }
예제 #15
0
 /// <summary>
 /// Reads a stream and converts the shapefile record to an equilivent geometry object.
 /// </summary>
 /// <param name="file">The stream to read.</param>
 /// <param name="geometryFactory">The geometry factory to use when making the object.</param>
 /// <returns>The Geometry object that represents the shape file record.</returns>
 public abstract IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory);
예제 #16
0
        /// <summary>
        /// Reads a generic stream containing geographic data saved as shapefile structure,
        /// and returns a collection of all the features contained.
        /// Since NTS Geometry Model not support Z and M data, those informations are ignored if presents in shapefile.
        /// </summary>
        /// <param name="stream">Shapefile data stream.</param>
        /// <returns><c>GeometryCollection</c> containing all geometries in shapefile.</returns>
        protected IGeometryCollection Read(Stream stream)
        {
            // Read big endian values
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Verify File Code
                int fileCode = beReader.ReadInt32BE();
                Debug.Assert(fileCode == 9994);

                stream.Seek(20, SeekOrigin.Current);
                length = beReader.ReadInt32BE();

                // Read little endian values
                using (BinaryReader leReader = new BinaryReader(stream))
                {
                    ArrayList list = null;

                    // Verify Version
                    int version = leReader.ReadInt32();
                    Debug.Assert(version == 1000);

                    // ShapeTypes
                    int shapeType = leReader.ReadInt32();

                    switch ((ShapeGeometryTypes)shapeType)
                    {
                    case ShapeGeometryTypes.Point:
                    case ShapeGeometryTypes.PointZ:
                    case ShapeGeometryTypes.PointM:
                    case ShapeGeometryTypes.PointZM:
                        list = new ArrayList(ReadPointData(stream));
                        break;

                    case ShapeGeometryTypes.LineString:
                    case ShapeGeometryTypes.LineStringZ:
                    case ShapeGeometryTypes.LineStringM:
                    case ShapeGeometryTypes.LineStringZM:
                        list = new ArrayList(ReadLineStringData(stream));
                        break;

                    case ShapeGeometryTypes.Polygon:
                    case ShapeGeometryTypes.PolygonZ:
                    case ShapeGeometryTypes.PolygonM:
                    case ShapeGeometryTypes.PolygonZM:
                        list = new ArrayList(ReadPolygonData(stream));
                        break;

                    case ShapeGeometryTypes.MultiPoint:
                    case ShapeGeometryTypes.MultiPointZ:
                    case ShapeGeometryTypes.MultiPointM:
                    case ShapeGeometryTypes.MultiPointZM:
                        list = new ArrayList(ReadMultiPointData(stream));
                        break;

                    case ShapeGeometryTypes.MultiPatch:
                        throw new NotImplementedException("FeatureType " + shapeType + " not supported.");

                    default:
                        throw new ArgumentOutOfRangeException("FeatureType " + shapeType + " not recognized by the system");
                    }
                    IGeometryCollection collection = shapeReader.CreateGeometryCollection(list);
                    return(collection);
                }
            }
        }
예제 #17
0
		/// <summary>
		/// Initializes a new instance of the Shapefile class with the given parameters.
		/// </summary>
		/// <param name="filename">The filename of the shape file to read (with .shp).</param>
		/// <param name="geometryFactory">The GeometryFactory to use when creating Geometry objects.</param>
		public ShapefileReader(string filename, IGeometryFactory geometryFactory)
		{           
			if (filename == null)
				throw new ArgumentNullException("filename");
			if (geometryFactory == null)
				throw new ArgumentNullException("geometryFactory");
			
            _filename = filename;
            _geometryFactory = geometryFactory;					

			// read header information. note, we open the file, read the header information and then
			// close the file. This means the file is not opened again until GetEnumerator() is requested.
			// For each call to GetEnumerator() a new BinaryReader is created.
			FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
			BigEndianBinaryReader shpBinaryReader = new BigEndianBinaryReader(stream);
			_mainHeader = new ShapefileHeader(shpBinaryReader);
			shpBinaryReader.Close();
		}
예제 #18
0
        /// <summary>
        /// Reads MultiPoint shapefile
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected IList ReadMultiPointData(Stream stream)
        {
            IList list = new ArrayList();

            // Jump to first header                
            stream.Seek(100, SeekOrigin.Begin); 

            // Read big endian informations
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Read little endian informations
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    // For each header                
                    while (stream.Position < stream.Length)
                    {
                        ReadFeatureHeader(beReader);
                        shapeReader.ReadBoundingBox(reader);

                        int numPoints = shapeReader.ReadNumPoints(reader);
                        ICoordinate[] coords = new ICoordinate[numPoints];
                        for (int i = 0; i < numPoints; i++)
                            coords[i] = shapeReader.ReadCoordinate(reader);
                        list.Add(shapeReader.CreateMultiPoint(coords));
                    }
             
                }
            }
            return list;
        }        
예제 #19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="beReader"></param>
 private void ReadFeatureHeader(BigEndianBinaryReader beReader)
 {
     int recordNumber  = beReader.ReadInt32BE();
     int contentLength = beReader.ReadInt32BE();
     int shapeType     = beReader.ReadInt32();
 }
예제 #20
0
 public static string[] ParseTldList(byte[] tldenc)
 {
     BigEndianBinaryReader br = new BigEndianBinaryReader(new MemoryStream(tldenc));
     int length = br.ReadInt32();
     List<char[]> tldList = new List<char[]>();
     int[] tldArray = new int[length];
     for(int id = 0; id < length; id++)
     {
         tldArray[id] = br.ReadByte();
         char[] tld = new char[br.ReadByte()];
         for (int charID = 0; charID < tld.Length; charID++)
             tld[charID] = (char)br.ReadByte();
         tldList.Add(tld);
     }
     string[] s = new string[tldList.Count];
     for (int c = 0; c < s.Length; c++)
         s[c] = new string(tldList[c]);
     return s;
 }
예제 #21
0
 public static int[] ParseFragmentsEnc(byte[] fragenc)
 {
     BigEndianBinaryReader br = new BigEndianBinaryReader(new MemoryStream(fragenc));
     int length = br.ReadInt32();
     int[] fragmentsEnc = new int[length];
     for (int i = 0; i < length; i++)
         fragmentsEnc[i] = br.ReadInt16();
     return fragmentsEnc;
 }
예제 #22
0
 public static string[] ParseDomainEnc(byte[] domainenc)
 {
     BigEndianBinaryReader br = new BigEndianBinaryReader(new MemoryStream(domainenc));
     int length = br.ReadInt32();
     List<char[]> domains = new List<char[]>();
     for (int j = 0; j < length; j++)
     {
         char[] val = new char[br.ReadByte()];
         for (int k = 0; k < val.GetLength(0); k++)
             val[k] = (char)br.ReadChar();
         domains.Add(val);
     }
     string[] s = new string[domains.Count];
     for (int c = 0; c < s.Length; c++)
         s[c] = new string(domains[c]);
     return s;
 }
예제 #23
0
 public static string[] ParseBadEnc(byte[] badenc)
 {
     BigEndianBinaryReader br = new BigEndianBinaryReader(new MemoryStream(badenc));
     int length = br.ReadInt32();
     List<char[]> bads = new List<char[]>();
     for (int i = 0; i < length; i++)
     {
         char[] val = new char[br.ReadChar()];
         for (int charid = 0; charid < val.Length; charid++)
             val[charid] = (char)br.ReadChar();
         bads.Add(val);
         byte[,] b1 = new byte[br.ReadByte(), 2];
         for (int l = 0; l < b1.Length/2; l++)
         {
             b1[l, 0] = (byte)br.ReadByte();
             b1[l, 1] = (byte)br.ReadByte();
         }
     }
     string[] s = new string[bads.Count];
     for(int c=0;c<s.Length;c++)
         s[c] = new string(bads[c]);
     return s;
 }
예제 #24
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="beReader"></param>
 private void ReadFeatureHeader(BigEndianBinaryReader beReader)
 {
     int recordNumber = beReader.ReadInt32BE();
     int contentLength = beReader.ReadInt32BE();
     int shapeType = beReader.ReadInt32();
 }        
예제 #25
0
        /// <summary>
        /// Reads a stream and converts the shapefile record to an equilivent geometry object.
        /// </summary>
        /// <param name="file">The stream to read.</param>
        /// <param name="geometryFactory">The geometry factory to use when making the object.</param>
        /// <returns>The Geometry object that represents the shape file record.</returns>
        public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory)
        {
            int shapeTypeNum             = file.ReadInt32();
            ShapeGeometryTypes shapeType = (ShapeGeometryTypes)Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString());

            if (!(shapeType == ShapeGeometryTypes.Polygon || shapeType == ShapeGeometryTypes.PolygonM ||
                  shapeType == ShapeGeometryTypes.PolygonZ || shapeType == ShapeGeometryTypes.PolygonZM))
            {
                throw new ShapefileException("Attempting to load a non-polygon as polygon.");
            }

            // Read and for now ignore bounds.
            double[] box = new double[4];
            for (int i = 0; i < 4; i++)
            {
                box[i] = file.ReadDouble();
            }

            int[] partOffsets;
            int   numParts  = file.ReadInt32();
            int   numPoints = file.ReadInt32();

            partOffsets = new int[numParts];
            for (int i = 0; i < numParts; i++)
            {
                partOffsets[i] = file.ReadInt32();
            }

            ArrayList shells = new ArrayList();
            ArrayList holes  = new ArrayList();

            int start, finish, length;

            for (int part = 0; part < numParts; part++)
            {
                start = partOffsets[part];
                if (part == numParts - 1)
                {
                    finish = numPoints;
                }
                else
                {
                    finish = partOffsets[part + 1];
                }
                length = finish - start;
                CoordinateList points = new CoordinateList();
                points.Capacity = length;
                for (int i = 0; i < length; i++)
                {
                    ICoordinate external = new Coordinate(file.ReadDouble(), file.ReadDouble());
                    geometryFactory.PrecisionModel.MakePrecise(external);
                    ICoordinate internalCoord = external;

                    // Thanks to Abhay Menon!
                    if (!Double.IsNaN(internalCoord.Y) && !Double.IsNaN(internalCoord.X))
                    {
                        points.Add(internalCoord, false);
                    }
                }

                if (points.Count > 2) // Thanks to Abhay Menon!
                {
                    try
                    {
                        if (points[0].Distance(points[points.Count - 1]) > .00001)
                        {
                            points.Add(new Coordinate(points[0]));
                        }
                        else if (points[0].Distance(points[points.Count - 1]) > 0.0)
                        {
                            points[points.Count - 1].CoordinateValue = points[0];
                        }

                        ILinearRing ring = geometryFactory.CreateLinearRing(points.ToArray());

                        // If shape have only a part, jump orientation check and add to shells
                        if (numParts == 1)
                        {
                            shells.Add(ring);
                        }
                        else
                        {
                            // Orientation check
                            if (CGAlgorithms.IsCCW(points.ToArray()))
                            {
                                holes.Add(ring);
                            }
                            else
                            {
                                shells.Add(ring);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }

            // Now we have a list of all shells and all holes
            ArrayList holesForShells = new ArrayList(shells.Count);

            for (int i = 0; i < shells.Count; i++)
            {
                holesForShells.Add(new ArrayList());
            }
            // Find holes
            for (int i = 0; i < holes.Count; i++)
            {
                ILinearRing testRing = (ILinearRing)holes[i];
                ILinearRing minShell = null;
                IEnvelope   minEnv   = null;
                IEnvelope   testEnv  = testRing.EnvelopeInternal;
                ICoordinate testPt   = testRing.GetCoordinateN(0);
                ILinearRing tryRing;
                for (int j = 0; j < shells.Count; j++)
                {
                    tryRing = (ILinearRing)shells[j];
                    IEnvelope tryEnv = tryRing.EnvelopeInternal;
                    if (minShell != null)
                    {
                        minEnv = minShell.EnvelopeInternal;
                    }
                    bool           isContained = false;
                    CoordinateList coordList   = new CoordinateList(tryRing.Coordinates);
                    if (tryEnv.Contains(testEnv) &&
                        (CGAlgorithms.IsPointInRing(testPt, coordList.ToArray()) ||
                         (PointInList(testPt, coordList))))
                    {
                        isContained = true;
                    }

                    // Check if this new containing ring is smaller than the current minimum ring
                    if (isContained)
                    {
                        if (minShell == null || minEnv.Contains(tryEnv))
                        {
                            minShell = tryRing;
                        }

                        // Suggested by Brian Macomber and added 3/28/2006:
                        // holes were being found but never added to the holesForShells array
                        // so when converted to geometry by the factory, the inner rings were never created.
                        ArrayList holesForThisShell = (ArrayList)holesForShells[j];
                        holesForThisShell.Add(testRing);
                    }
                }
            }

            IPolygon[] polygons = new IPolygon[shells.Count];
            for (int i = 0; i < shells.Count; i++)
            {
                polygons[i] = (geometryFactory.CreatePolygon((ILinearRing)shells[i],
                                                             (ILinearRing[])((ArrayList)holesForShells[i]).ToArray(typeof(ILinearRing))));
            }

            if (polygons.Length == 1)
            {
                return(polygons[0]);
            }
            // It's a multi part
            return(geometryFactory.CreateMultiPolygon(polygons));
        }
예제 #26
0
        /// <summary>
        /// Reads Polygon shapefile
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected IList ReadPolygonData(Stream stream)
        {
            IList list = new ArrayList();

            // Jump to first header                
            stream.Seek(100, SeekOrigin.Begin); 

            // Read big endian informations
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Read little endian informations
                using (BinaryReader reader = new BinaryReader(stream))
                {                         
                    // For each header                
                    while (stream.Position < stream.Length)
                    {
                        ReadFeatureHeader(beReader);
                        shapeReader.ReadBoundingBox(reader);

                        int[] indexParts = null;
                        int numParts = shapeReader.ReadNumParts(reader);
                        int numPoints = shapeReader.ReadNumPoints(reader);

                        indexParts = shapeReader.ReadIndexParts(reader, numParts);
                        ICoordinate[] coordinates = shapeReader.ReadCoordinates(reader, numPoints);

                        if (numParts == 1)
                             list.Add(shapeReader.CreateSimpleSinglePolygon(coordinates));
                        else list.Add(shapeReader.CreateSingleOrMultiPolygon(numPoints, indexParts, coordinates));
                    }                    
                }
            }
            return list;
        }
예제 #27
0
 public static NPCConfig[] ParseNPCConfig(byte[] data, byte[] index)
 {
     BigEndianBinaryReader dataReader = new BigEndianBinaryReader(new MemoryStream(data));
     BigEndianBinaryReader indexReader = new BigEndianBinaryReader(new MemoryStream(index));
     int totalNPCs = indexReader.ReadUInt16();
     NPCConfig[] NPCList = new NPCConfig[totalNPCs];
     int[] streamIndices = new int[totalNPCs];
     int offset = 2;
     for (int npcPtr = 0; npcPtr < totalNPCs; npcPtr++)
     {
         streamIndices[npcPtr] = offset;
         offset += indexReader.ReadUInt16();
     }
     for (int j = 0; j < totalNPCs; j++)
     {
         NPCConfig npc = new NPCConfig();
         dataReader.BaseStream.Position = streamIndices[j];
         do
         {
             byte i = dataReader.ReadByte();
             if (i == 0)
                 break;
             else if (i == 1)
             {
                 int modelCount = dataReader.ReadByte();
                 npc.npcModels = new int[modelCount];
                 for (int k = 0; k < modelCount; k++)
                     npc.npcModels[k] = dataReader.ReadUInt16();
             }
             else if (i == 2)
                 npc.name = dataReader.ReadString();
             else if (i == 3)
                 npc.description = dataReader.ReadString();
             else if (i == 12)
                 npc.boundDim = dataReader.ReadByte();
             else if (i == 13)
                 npc.idleAnimation = dataReader.ReadUInt16();
             else if (i == 14)
                 npc.walkAnimIndex = dataReader.ReadUInt16();
             else if (i == 17)
             {
                 npc.walkAnimIndex = dataReader.ReadUInt16();
                 npc.turn180AnimIndex = dataReader.ReadUInt16();
                 npc.turn90CWAnimIndex = dataReader.ReadUInt16();
                 npc.turn90CCWAnimIndex = dataReader.ReadUInt16();
             }
             else if (i >= 30 && i < 40)
             {
                 if (npc.actions == null)
                     npc.actions = new string[5];
                 npc.actions[i - 30] = dataReader.ReadString();
                 if (npc.actions[i - 30] == "hidden")
                     npc.actions[i - 30] = null;
             }
             else if (i == 40)
             {
                 int colors = dataReader.ReadByte();
                 npc.recolorOriginal = new int[colors];
                 npc.recolorTarget = new int[colors];
                 for (int l = 0; l < colors; l++)
                 {
                     npc.recolorOriginal[l] = dataReader.ReadUInt16();
                     npc.recolorTarget[l] = dataReader.ReadUInt16();
                 }
             }
             else if (i == 60)
             {
                 int additionalModelCount = dataReader.ReadByte();
                 npc.additionalModels = new int[additionalModelCount];
                 for (int l = 0; l < additionalModelCount; l++)
                     npc.additionalModels[l] = dataReader.ReadUInt16();
             }
             else if (i >= 90 && i < 93)
                 dataReader.ReadUInt16();
             else if (i == 93)
                 npc.drawMinimapDot = false;
             else if (i == 95)
                 npc.combatLevel = dataReader.ReadUInt16();
             else if (i == 97)
                 npc.scaleXZ = dataReader.ReadUInt16();
             else if (i == 98)
                 npc.scaleY = dataReader.ReadUInt16();
             else if (i == 99)
                 npc.invisible = true;
             else if (i == 100)
                 npc.lightModifier = dataReader.ReadByte();
             else if (i == 101)
                 npc.shadowModifier = dataReader.ReadByte() * 5;
             else if (i == 102)
                 npc.headIcon = dataReader.ReadUInt16();
             else if (i == 103)
                 npc.degreesToTurn = dataReader.ReadUInt16();
             else if (i == 106)
             {
                 npc.varBitID = dataReader.ReadUInt16();
                 if (npc.varBitID == 65535)
                     npc.varBitID = -1;
                 npc.sessionSettingID = dataReader.ReadUInt16();
                 if (npc.sessionSettingID == 65535)
                     npc.sessionSettingID = -1;
                 int childrensCount = dataReader.ReadByte();
                 npc.childrenIDs = new int[childrensCount + 1];
                 for (int c = 0; c <= childrensCount; c++)
                 {
                     npc.childrenIDs[c] = dataReader.ReadUInt16();
                     if (npc.childrenIDs[c] == 65535)
                         npc.childrenIDs[c] = -1;
                 }
             }
             else if (i == 107)
                 npc.clickable = false;
         }
         while (true);
         NPCList[j] = npc;
     }
     return NPCList;
 }
예제 #28
0
        /// <summary>
        /// Reads a generic stream containing geographic data saved as shapefile structure, 
        /// and returns a collection of all the features contained.
        /// Since NTS Geometry Model not support Z and M data, those informations are ignored if presents in shapefile.
        /// </summary>
        /// <param name="stream">Shapefile data stream.</param>
        /// <returns><c>GeometryCollection</c> containing all geometries in shapefile.</returns>
        protected IGeometryCollection Read(Stream stream)
        {
            // Read big endian values
            using (BigEndianBinaryReader beReader = new BigEndianBinaryReader(stream))
            {
                // Verify File Code
                int fileCode = beReader.ReadInt32BE();
                Debug.Assert(fileCode == 9994);

                stream.Seek(20, SeekOrigin.Current);
                length = beReader.ReadInt32BE();                
                
                // Read little endian values
                using (BinaryReader leReader = new BinaryReader(stream))
                {
                    ArrayList list = null;

                    // Verify Version
                    int version = leReader.ReadInt32();
                    Debug.Assert(version == 1000);

                    // ShapeTypes
                    int shapeType = leReader.ReadInt32();         

                    switch ((ShapeGeometryType) shapeType)
                    {
                        case ShapeGeometryType.Point:
                        case ShapeGeometryType.PointZ:
                        case ShapeGeometryType.PointM:
                        case ShapeGeometryType.PointZM:
                            list = new ArrayList(ReadPointData(stream));
                            break;

                        case ShapeGeometryType.LineString:
                        case ShapeGeometryType.LineStringZ:
                        case ShapeGeometryType.LineStringM:
                        case ShapeGeometryType.LineStringZM:
                            list = new ArrayList(ReadLineStringData(stream));
                            break;

                        case ShapeGeometryType.Polygon:
                        case ShapeGeometryType.PolygonZ:
                        case ShapeGeometryType.PolygonM:
                        case ShapeGeometryType.PolygonZM:
                            list = new ArrayList(ReadPolygonData(stream));
                            break;

                        case ShapeGeometryType.MultiPoint:
                        case ShapeGeometryType.MultiPointZ:
                        case ShapeGeometryType.MultiPointM:
                        case ShapeGeometryType.MultiPointZM:
                            list = new ArrayList(ReadMultiPointData(stream));
                            break;

                        case ShapeGeometryType.MultiPatch:
                            throw new NotImplementedException("FeatureType " + shapeType + " not supported.");

                        default:
                            throw new ArgumentOutOfRangeException("FeatureType " + shapeType + " not recognized by the system");
                    }
                    IGeometryCollection collection = shapeReader.CreateGeometryCollection(list);
                    return collection;
                }                               
            }
        }             
예제 #29
0
 public static ObjectConfig[] ParseObjectConfig(byte[] data, byte[] index)
 {
     BigEndianBinaryReader indexReader = new BigEndianBinaryReader(new MemoryStream(index));
     BigEndianBinaryReader dataReader = new BigEndianBinaryReader(new MemoryStream(data));
     ObjectConfig[] obj = new ObjectConfig[indexReader.ReadUInt16()];
     int sIndex = 2;
     int[] streamIndices = new int[obj.Length];
     for (int i = 0; i < streamIndices.Length; i++)
     {
         streamIndices[i] = sIndex;
         sIndex += indexReader.ReadUInt16();
     }
     for (int j = 0; j < obj.Length; j++)
     {
         if (obj[j] == null) obj[j] = new ObjectConfig();
         dataReader.BaseStream.Position = streamIndices[j];
         bool breakTop = false;
         bool continueTop = false;
         int t = 0;
         do
         {
             continueTop = false;
             int k;
             do
             {
                 k = dataReader.ReadByte();
                 if (k == 0)
                     breakTop = true;
                 else if (k == 1)
                 {
                     int l = dataReader.ReadByte();
                     if (l > 0)
                         if (obj[j].objectModelIDs == null)
                         {
                             obj[j].types = new int[l];
                             obj[j].objectModelIDs = new int[l];
                             for (int k1 = 0; k1 < l; k1++)
                             {
                                 obj[j].objectModelIDs[k1] = dataReader.ReadUInt16();
                                 obj[j].types[k1] = dataReader.ReadByte();
                             }
                         }
                         else
                             dataReader.BaseStream.Position += l * 3;
                 }
                 else if (k == 2)
                     obj[j].name = dataReader.ReadString();
                 else if (k == 3)
                     obj[j].description = dataReader.ReadString();
                 else if (k == 5)
                 {
                     int l = dataReader.ReadByte();
                     if (l > 0)
                         if (obj[j].objectModelIDs == null)
                         {
                             obj[j].types = null;
                             obj[j].objectModelIDs = new int[l];
                             for (int l1 = 0; l1 < l; l1++)
                                 obj[j].objectModelIDs[l1] = dataReader.ReadUInt16();
                         }
                         else dataReader.BaseStream.Position += l * 2;
                 }
                 else if (k == 14)
                     obj[j].sizeX = dataReader.ReadByte();
                 else if (k == 15)
                     obj[j].sizeY = dataReader.ReadByte();
                 else if (k == 17)
                     obj[j].isUnwalkable = false;
                 else if (k == 18)
                     obj[j].aBoolean757 = false;
                 else if (k == 19)
                 {
                     t = dataReader.ReadByte();
                     if (t == 1)
                         obj[j].hasActions = true;
                 }
                 else if (k == 21)
                     obj[j].adjustToTerrain = true;
                 else if (k == 22)
                     obj[j].nonFlatShading = true;
                 else if (k == 23)
                     obj[j].aBoolean764 = true;
                 else if (k == 24)
                 {
                     obj[j].animationID = dataReader.ReadUInt16();
                     if (obj[j].animationID == 65535) obj[j].animationID = -1;
                 }
                 else if (k == 28)
                     obj[j].anInt775 = dataReader.ReadByte();
                 else if (k == 29)
                     obj[j].brightness = dataReader.ReadByte();
                 else if (k == 39)
                     obj[j].contrast = dataReader.ReadByte();
                 else if (k >= 30 && k < 39)
                 {
                     if (obj[j].actions == null)
                         obj[j].actions = new string[10];
                     obj[j].actions[k - 30] = dataReader.ReadString();
                     if (obj[j].actions[k - 30].ToLower() == "hidden")
                         obj[j].actions[k - 30] = null;
                 }
                 else if (k == 40)
                 {
                     int i1 = dataReader.ReadByte();
                     obj[j].modifiedModelColors = new int[i1];
                     obj[j].originalModelColors = new int[i1];
                     for (int i2 = 0; i2 < i1; i2++)
                     {
                         obj[j].modifiedModelColors[i2] = dataReader.ReadUInt16();
                         obj[j].originalModelColors[i2] = dataReader.ReadUInt16();
                     }
                 }
                 else if (k == 60)
                     obj[j].mapFunctionID = dataReader.ReadUInt16();
                 else if (k == 62)
                     obj[j].aBoolean751 = true;
                 else if (k == 64)
                     obj[j].aBoolean779 = false;
                 else if (k == 65)
                     obj[j].modelSizeX = dataReader.ReadUInt16();
                 else if (k == 66)
                     obj[j].modelSizeH = dataReader.ReadUInt16();
                 else if (k == 67)
                     obj[j].modelSizeY = dataReader.ReadUInt16();
                 else if (k == 68)
                     obj[j].mapSceneID = dataReader.ReadUInt16();
                 else if (k == 69)
                     obj[j].anInt768 = dataReader.ReadByte();
                 else if (k == 70)
                     obj[j].offsetX = dataReader.ReadUInt16();
                 else if (k == 71)
                     obj[j].offsetH = dataReader.ReadUInt16();
                 else if (k == 72)
                     obj[j].offsetY = dataReader.ReadUInt16();
                 else if (k == 73)
                     obj[j].aBoolean736 = true;
                 else if (k == 74)
                     obj[j].isSolidObject = true;
                 else
                 {
                     if (k != 75)
                         continue;
                     obj[j].anInt760 = dataReader.ReadByte();
                 }
                 continueTop = true;
             } while (k != 77 && !breakTop);
             if (breakTop) break;
             if (continueTop) continue;
             obj[j].configId_1 = dataReader.ReadUInt16();
             if (obj[j].configId_1 == 65535)
                 obj[j].configId_1 = -1;
             obj[j].configId_1 = dataReader.ReadUInt16();
             if (obj[j].configID == 65535)
                 obj[j].configID = -1;
             int j1 = dataReader.ReadByte();
             obj[j].configObjectIDs = new int[j1 + 1];
             for (int j2 = 0; j2 <= j1; j2++)
             {
                 obj[j].configObjectIDs[j2] = dataReader.ReadUInt16();
                 if (obj[j].configObjectIDs[j2] == 65535)
                     obj[j].configObjectIDs[j2] = -1;
             }
         } while (!breakTop);
         if (t == -1)
         {
             obj[j].hasActions = obj[j].objectModelIDs != null;
             if (obj[j].actions != null)
                 obj[j].hasActions = true;
         }
         if (obj[j].isSolidObject)
             obj[j].isUnwalkable = obj[j].aBoolean757 = false;
         if (obj[j].anInt760 == -1)
             obj[j].anInt760 = obj[j].isUnwalkable ? 1 : 0;
     }
     return obj;
 }
예제 #30
0
		/// <summary>
		/// Reads a stream and converts the shapefile record to an equilivent geometry object.
		/// </summary>
		/// <param name="file">The stream to read.</param>
		/// <param name="geometryFactory">The geometry factory to use when making the object.</param>
		/// <returns>The Geometry object that represents the shape file record.</returns>
		public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory)
		{
			int shapeTypeNum = file.ReadInt32();
            ShapeGeometryTypes shapeType = (ShapeGeometryTypes)Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString());
            if ( ! ( shapeType == ShapeGeometryTypes.Polygon  || shapeType == ShapeGeometryTypes.PolygonM ||
                     shapeType == ShapeGeometryTypes.PolygonZ || shapeType == ShapeGeometryTypes.PolygonZM))	
				throw new ShapefileException("Attempting to load a non-polygon as polygon.");

			// Read and for now ignore bounds.
			double[] box = new double[4];
			for (int i = 0; i < 4; i++) 
				box[i] = file.ReadDouble();

			int[] partOffsets;        
			int numParts = file.ReadInt32();
			int numPoints = file.ReadInt32();
			partOffsets = new int[numParts];
			for (int i = 0; i < numParts; i++)
				partOffsets[i] = file.ReadInt32();

			ArrayList shells = new ArrayList();
			ArrayList holes = new ArrayList();

			int start, finish, length;
			for (int part = 0; part < numParts; part++)
			{
				start = partOffsets[part];
				if (part == numParts - 1)
					 finish = numPoints;
				else finish = partOffsets[part + 1];
				length = finish - start;
                CoordinateList points = new CoordinateList();
				points.Capacity = length;
				for (int i = 0; i < length; i++)
				{
					ICoordinate external = new Coordinate(file.ReadDouble(), file.ReadDouble() );					
                    geometryFactory.PrecisionModel.MakePrecise( external);
                    ICoordinate internalCoord = external;

                    // Thanks to Abhay Menon!
                    if (!Double.IsNaN(internalCoord.Y) && !Double.IsNaN(internalCoord.X))
                         points.Add(internalCoord, false);
 				}

                if (points.Count > 2) // Thanks to Abhay Menon!
                {
                    try
                    {
                        if (points[0].Distance(points[points.Count - 1]) > .00001)
                            points.Add(new Coordinate(points[0]));
                        else if (points[0].Distance(points[points.Count - 1]) > 0.0)
                            points[points.Count - 1].CoordinateValue = points[0];

                        ILinearRing ring = geometryFactory.CreateLinearRing(points.ToArray());

                        // If shape have only a part, jump orientation check and add to shells
                        if (numParts == 1)
                            shells.Add(ring);
                        else
                        {
                            // Orientation check
                            if (CGAlgorithms.IsCCW(points.ToArray()))
                                holes.Add(ring);
                            else shells.Add(ring);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
			}

			// Now we have a list of all shells and all holes
			ArrayList holesForShells = new ArrayList(shells.Count);
			for (int i = 0; i < shells.Count; i++)
				holesForShells.Add(new ArrayList());
			// Find holes
			for (int i = 0; i < holes.Count; i++)
			{
				ILinearRing testRing = (ILinearRing) holes[i];
				ILinearRing minShell = null;
				IEnvelope minEnv = null;
                IEnvelope testEnv = testRing.EnvelopeInternal;
                ICoordinate testPt = testRing.GetCoordinateN(0);
				ILinearRing tryRing;
				for (int j = 0; j < shells.Count; j++)
				{
					tryRing = (ILinearRing) shells[j];
                    IEnvelope tryEnv = tryRing.EnvelopeInternal;
					if (minShell != null)
                        minEnv = minShell.EnvelopeInternal;
					bool isContained = false;
					CoordinateList coordList = new CoordinateList(tryRing.Coordinates);
					if (tryEnv.Contains(testEnv)
                        && (CGAlgorithms.IsPointInRing(testPt, coordList.ToArray()) 
                        || (PointInList(testPt, coordList)))) 				
						isContained = true;

                    // Check if this new containing ring is smaller than the current minimum ring
                    if (isContained)
                    {
                        if (minShell == null || minEnv.Contains(tryEnv))
                            minShell = tryRing;             

                        // Suggested by Brian Macomber and added 3/28/2006:
                        // holes were being found but never added to the holesForShells array
                        // so when converted to geometry by the factory, the inner rings were never created.
                        ArrayList holesForThisShell = (ArrayList) holesForShells[j];
                        holesForThisShell.Add(testRing);
                    }
				}
			}

			IPolygon[] polygons = new IPolygon[shells.Count];
			for (int i = 0; i < shells.Count; i++)
                polygons[i] = (geometryFactory.CreatePolygon((ILinearRing) shells[i], 
                    (ILinearRing[]) ((ArrayList)holesForShells[i]).ToArray(typeof(ILinearRing))));
        
			if (polygons.Length == 1)
				return polygons[0];
			// It's a multi part
			return geometryFactory.CreateMultiPolygon(polygons);
		}
예제 #31
0
 public static ItemConfig[] ParseItemConfig(byte[] data, byte[] index)
 {
     BigEndianBinaryReader dataReader = new BigEndianBinaryReader(new MemoryStream(data));
     BigEndianBinaryReader indexReader = new BigEndianBinaryReader(new MemoryStream(index));
     int totalItems = indexReader.ReadUInt16();
     int[] streamIndices = new int[totalItems];
     ItemConfig[] itemConfig = new ItemConfig[totalItems];
     int i = 2;
     for (int j = 0; j < totalItems; j++)
     {
         streamIndices[j] = i;
         i += indexReader.ReadUInt16();
     }
     for (int x = 0; x < totalItems; x++)
     {
         dataReader.BaseStream.Position = streamIndices[x];
         ItemConfig item = new ItemConfig();
         do
         {
             int opCode = dataReader.ReadByte();
             if (opCode == 0)
                 break;
             if (opCode == 1)
                 item.modelID = dataReader.ReadUInt16();
             else if (opCode == 2)
                 item.name = dataReader.ReadString().TrimEnd('\n');
             else if (opCode == 3)
                 item.description = dataReader.ReadString().TrimEnd('\n');
             else if (opCode == 4)
                 item.modelInvZoom = dataReader.ReadUInt16();
             else if (opCode == 5)
                 item.modelInvRotationY = dataReader.ReadUInt16();
             else if (opCode == 6)
                 item.modelInvRotationX = dataReader.ReadUInt16();
             else if (opCode == 7)
             {
                 item.modelInvPosOffsetX = dataReader.ReadUInt16();
                 if (item.modelInvPosOffsetX > 32767)
                     item.modelInvPosOffsetX -= 0x10000;
             }
             else if (opCode == 8)
             {
                 item.modelInvPosOffsetY = dataReader.ReadUInt16();
                 if (item.modelInvPosOffsetY > 32767)
                     item.modelInvPosOffsetY -= 0x10000;
             }
             else if (opCode == 10)
                 dataReader.ReadUInt16();
             else if (opCode == 11)
                 item.stackable = true;
             else if (opCode == 12)
                 item.value = dataReader.ReadInt32();
             else if (opCode == 16)
                 item.membersObject = true;
             else if (opCode == 23)
             {
                 item.maleWornModelID = dataReader.ReadUInt16();
                 item.maleYOffset = dataReader.ReadByte();
             }
             else if (opCode == 24)
                 item.maleArmsID = dataReader.ReadUInt16();
             else if (opCode == 25)
             {
                 item.femaleWornModelID = dataReader.ReadUInt16();
                 item.femaleYOffset = dataReader.ReadByte();
             }
             else if (opCode == 26)
                 item.femaleArmsID = dataReader.ReadUInt16();
             else if (opCode >= 30 && opCode < 35)
             {
                 if (item.groundActions == null)
                     item.groundActions = new string[5];
                 item.groundActions[opCode - 30] = dataReader.ReadString().TrimEnd('\n');
                 if (item.groundActions[opCode - 30].ToLower() == "hidden")
                     item.groundActions[opCode - 30] = null;
             }
             else if (opCode >= 35 && opCode < 40)
             {
                 if (item.actions == null)
                     item.actions = new string[5];
                 item.actions[opCode - 35] = dataReader.ReadString().TrimEnd('\n');
             }
             else if (opCode == 40)
             {
                 int colors = dataReader.ReadByte();
                 item.originalModelColors = new int[colors];
                 item.modifiedModelColors = new int[colors];
                 for (int colorPtr = 0; colorPtr < colors; colorPtr++)
                 {
                     item.originalModelColors[colorPtr] = dataReader.ReadUInt16();
                     item.modifiedModelColors[colorPtr] = dataReader.ReadUInt16();
                 }
             }
             else if (opCode == 78)
                 item.maleEmblem = dataReader.ReadUInt16();
             else if (opCode == 79)
                 item.femaleEmblem = dataReader.ReadUInt16();
             else if (opCode == 90)
                 item.maleDialog = dataReader.ReadUInt16();
             else if (opCode == 91)
                 item.femaleDialog = dataReader.ReadUInt16();
             else if (opCode == 92)
                 item.maleDialogHat = dataReader.ReadUInt16();
             else if (opCode == 93)
                 item.femaleDialogHat = dataReader.ReadUInt16();
             else if (opCode == 95)
                 item.diagonalRotation = dataReader.ReadUInt16();
             else if (opCode == 97)
                 item.certID = dataReader.ReadUInt16();
             else if (opCode == 98)
                 item.certTemplateID = dataReader.ReadUInt16();
             else if (opCode >= 100 && opCode < 110)
             {
                 if (item.stackIDs == null)
                 {
                     item.stackIDs = new int[10];
                     item.stackAmounts = new int[10];
                 }
                 item.stackIDs[opCode - 100] = dataReader.ReadUInt16();
                 item.stackAmounts[opCode - 100] = dataReader.ReadUInt16();
             }
             else if (opCode == 110)
                 item.modelSizeX = dataReader.ReadUInt16();
             else if (opCode == 111)
                 item.modelSizeY = dataReader.ReadUInt16();
             else if (opCode == 112)
                 item.modelSizeZ = dataReader.ReadUInt16();
             else if (opCode == 113)
                 item.lightModifier = dataReader.ReadUInt16();
             else if (opCode == 114)
                 item.shadowModifier = dataReader.ReadByte() * 5;
             else if (opCode == 115)
                 item.team = dataReader.ReadByte();
             else if (opCode == 116)
                 item.lendID = dataReader.ReadUInt16();
             else if (opCode == 117)
                 item.lentItemID = dataReader.ReadUInt16();
             else
                 Logger.Log("Unknown Item Opcode: " + opCode, LogType.Error);
         } while (true);
         itemConfig[x] = item;
     }
     return itemConfig;
 }
예제 #32
0
 public ImageGroup(byte[] index, byte[] data, bool unpack)
 {
     indexReader = new BigEndianBinaryReader(new MemoryStream(index));
     dataReader = new BigEndianBinaryReader(new MemoryStream(data));
     indexReader.BaseStream.Position = dataReader.ReadUInt16();
     indexOffset = (int)indexReader.BaseStream.Position;
     maxWidth = indexReader.ReadUInt16();
     maxHeight = indexReader.ReadUInt16();
     colorCount = indexReader.ReadByte();
     colorMap = new int[colorCount];
     for (int x = 0; x < colorCount - 1; x++)
     {
         colorMap[x + 1] = indexReader.ReadUInt24();
         if (colorMap[x + 1] == 0)
             colorMap[x + 1] = 1;
     }
     if(unpack)
         unpackImages();
 }
예제 #33
0
		/// <summary>
		/// Reads a stream and converts the shapefile record to an equilivent geometry object.
		/// </summary>
		/// <param name="file">The stream to read.</param>
		/// <param name="geometryFactory">The geometry factory to use when making the object.</param>
		/// <returns>The Geometry object that represents the shape file record.</returns>
		public abstract IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory);