📄 studymapbuffer.java
字号:
package standalone;import com.mapinfo.mapj.MapJ;import com.mapinfo.mapj.FeatureLayer;import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;import com.mapinfo.dp.*;import com.mapinfo.dp.annotation.AnnotationDataProviderHelper;import com.mapinfo.dp.annotation.AnnotationTableDescHelper;import com.mapinfo.unit.Distance;import com.mapinfo.unit.LinearUnit;import com.mapinfo.util.GeometryUtils;import com.mapinfo.graphics.RenditionImpl;import com.mapinfo.graphics.Rendition;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Image;import java.awt.Color;import java.util.ArrayList;import com.mapinfo.dp.util.RewindableFeatureSet;import com.mapinfo.dp.util.FeatureImpl;import com.mapinfo.dp.util.GeometryImpl;import com.mapinfo.dp.util.LocalDataProviderRef;import java.awt.BorderLayout;import java.awt.Graphics;import com.mapinfo.util.DoublePoint;import com.mapinfo.util.DoubleRect;import com.mapinfo.mapj.DataLayer;import com.mapinfo.graphics.ColorStop;import java.util.List;import com.mapinfo.graphics.SpreadMethod;import com.mapinfo.graphics.LinearGradient;import com.mapinfo.graphics.RadialGradient;import java.awt.geom.Rectangle2D;public class StudyMapBuffer extends JFrame {public MapJ myMap;public Image myimage;public static final int NUM_OF_COLORS = ImageRequestComposer.MAX_COLORS_TRUECOLOR;public static final Color BACKGROUND_COLOR = Color.white;public static final String MapXtremeURL = "http://901-32:8083/mapxtreme47/mapxtreme"; public static final String gifURL="http://901-32:8083/mapxtreme47/gif/car.gif";public mapCanvass mc;public StudyMapBuffer(){ DoublePoint centerPoint=new DoublePoint(-108.8,42.5); LinearUnit unit=LinearUnit.kilometer ; DoubleRect bound=new DoubleRect(0,0,800,600); DataLayer dataLayer=null; try { myMap = new MapJ(); myMap.loadMapDefinition("e:\\MapInfo\\study\\mapmdf\\ADMINISTRATIVE.mdf"); dataLayer = (DataLayer)myMap.getLayers() .get(0); DoubleRect rect = dataLayer.getBounds(); double width = (rect.xmax - rect.xmin) / 1; double height = (rect.ymax - rect.ymin) / 1; myMap.setBounds(new DoubleRect(centerPoint, width, height)); myMap.setDeviceBounds(bound);//------------for adding a buffer------------------------------------ ArrayList columns = new ArrayList(); Attribute att1 = new Attribute("莲湖区"); FeatureLayer parkLayer = (FeatureLayer)myMap.getLayers().get("ADMINISTRATIVE"); parkLayer.setAutoLabel(true) ; FeatureSet fs = parkLayer.searchByAttribute(columns,"name",att1,null); RewindableFeatureSet rfs = new RewindableFeatureSet(fs); Feature feat = rfs.getNextFeature(); Geometry region = feat.getGeometry(); //获取该查找到的几何体员的位置使它能够显示在地图的中心 DoubleRect thisrect=region.getBounds() ; double centerx=(thisrect.xmax -thisrect.xmin )/2;//得到该几何体的相对中心位置:X double centery=(thisrect.ymax -thisrect.ymin )/2;//得到该几何体的相对中心位置:Y //自己的相当中心位置再加上以前的绝对中心位置就是现在的绝对中心位置 myMap.setCenter(new DoublePoint(centerx+thisrect.xmin ,centery+thisrect.ymin ));//compute region area & perimeter in miles double regionArea = GeometryUtils.area(region, LinearUnit.kilometer ); double regionPerimeter = GeometryUtils.perimeter(region, LinearUnit.kilometer ); System.out.println("Area of the region is: " + regionArea+" square KM"); System.out.println("Perimieter of the region is: " + regionPerimeter+" square KM"); rfs.rewind(); //... access the feature again to manufacture the buffer //之所以能够访问是因为用了rfs.rewind()语句,实际上feat2和feat一样 Feature feat2 = rfs.getNextFeature(); feat2=feat;//因为feat2和feat一样,所以我直接将它拷贝过来试验了一下,结果效果一样 Geometry region2 = feat2.getGeometry();// construct 2km external buffer on the region (use negative value, ie) -5 to create an "internal" buffer) Distance distance = new Distance(0.1, LinearUnit.kilometer); int resolution = 12;// 12 points of resolution VectorGeometry buffer = GeometryUtils.buffer(region2,//the source geometry to be buffered distance, //the size of the buffer,specifed as a scalar value assocated with a linear unit resolution);// Compute buffer area and perimeter in miles double bufferArea = GeometryUtils.area(buffer, LinearUnit.kilometer ); double bufferPerimeter = GeometryUtils.perimeter(buffer, LinearUnit.kilometer ); System.out.println("Area of the Buffer is: " + bufferArea+" square km"); System.out.println("Perimieter of the Buffer is: " + bufferPerimeter+" square km");// create Rendition for the buffer //use gif fill/* Rendition rendGif=new RenditionImpl(); rendGif.setValue(Rendition.SYMBOL_MODE ,Rendition.SymbolMode .IMAGE ); rendGif.setValue(Rendition.SYMBOL_URL ,gifURL);*/ //use linear gradient/* List colorStops=new ArrayList(); colorStops.add(new ColorStop(Color.red .brighter() ,0.25)); colorStops.add(new ColorStop(Color.red .darker() ,0.75)); DoublePoint startPoint=new DoublePoint(0,0); DoublePoint endPoint=new DoublePoint(1,0); LinearGradient linearGradient=new LinearGradient(colorStops,SpreadMethod.REPEAT ,startPoint,endPoint);*///use radial gradient List colorStops=new ArrayList(); colorStops.add(new ColorStop(Color.blue .brighter() ,0.25)) ; colorStops.add(new ColorStop(Color.blue .darker() ,0.75)) ; DoublePoint center=new DoublePoint(0.5,0.5); double radius=0.5; RadialGradient radialGradient=new RadialGradient(colorStops,SpreadMethod.PAD ,center,radius); // //use Rendition.STROKE Rendition rend = RenditionImpl.getDefaultRendition(); /* rend.setValue(Rendition.STROKE ,Color.red ) ; rend.setValue(Rendition.STROKE_WIDTH ,3.2f) ; rend.setValue(Rendition.STROKE_OPACITY ,0.5f) ; */ //// rend.setValue(Rendition.FILL ,radialGradient) ; // rend.setValue(Rendition.FILL ,linearGradient) ;//study how to use font sumbol/*Rendition rendFontSymbol=new RenditionImpl();rendFontSymbol.setValue(Rendition.SYMBOL_MODE ,Rendition.SymbolMode .FONT );rendFontSymbol.setValue(Rendition.FONT_FAMILY ,"Mapinfo Cartographic") ;rendFontSymbol.setValue(Rendition.FONT_SIZE ,16);//rendFontSymbol.setValue(Rendition.SYMBOL_STRING ,String.valueOf((char)33)) ;rendFontSymbol.setValue(Rendition.SYMBOL_STRING ,"1") ;*///study how to use symbol vector Rendition rendShape=new RenditionImpl(); rendShape.setValue(Rendition.STROKE ,Color.red ); // rendShape.setValue(Rendition.FILL ,Color.red); Rectangle2D rect2D=new Rectangle2D.Float(0,0,10,10); Rendition.SymbolShape symbolShape=new Rendition.SymbolShape(rect2D,rendShape); rend.setValue(Rendition.SYMBOL_MODE ,Rendition.SymbolMode .SHAPE ); rend.setValue(Rendition.SYMBOL_SHAPE ,symbolShape); // rend.setValue(Rendition.FILL ,Color.red ) ; //// rend.setValue(Rendition.FILL_OPACITY, new Float(0.5));// Create an attributes array for the feature Attribute [] attrs = new Attribute[1]; attrs[0] = new Attribute("att1");//A feature can be made this way, using the FeatureImpl Class found in the MXTJ 4.7 DataProvider API Feature myBufferFeature = new FeatureImpl(new PrimaryKey(attrs[0]),//the priamry key of the new feature (GeometryImpl)buffer,//the geometry of the new feature attrs,//attribute array rend,//the rendition for this feature null,// null);//the raster for this //下面这个是练习,练习如何得到feature对应的列值,因为一个层实际上对应的就是一个表,所以一个feature //对应的应该就是一列,这个feature是新建的,不是从 featureLayer中得到的,所以getAttribute(i)也就只得到了 //新建该feature时所给的属性数据,注意getAttribute()方法只能得到属性数据,得不到空间数据.要想得到空间数据可以 //用getGeometry()方法 for (int i = 0; i < myBufferFeature.getAttributeCount(); i++) { Attribute att =myBufferFeature.getAttribute(i); String str = att.getString(); str = "wait"; } AnnotationTableDescHelper annTDHelper = new AnnotationTableDescHelper("AnnLayer"); AnnotationDataProviderHelper annDPHelper = new AnnotationDataProviderHelper(); LocalDataProviderRef localDPRef = new LocalDataProviderRef(annDPHelper); FeatureLayer AnnoLayer = (FeatureLayer)myMap.getLayers().insertLayer(localDPRef, annTDHelper,0,"AnnLayer"); AnnoLayer.addFeature(myBufferFeature);//AnnoLayer.setAutoLabel(true) ;// Toss the FeatureSet when done with it fs.dispose();//--------------------------------------------------------------- ImageRequestComposer irc = ImageRequestComposer.create(myMap, NUM_OF_COLORS, BACKGROUND_COLOR, "image/png"); MapXtremeImageRenderer renderer = new MapXtremeImageRenderer(MapXtremeURL); renderer.render(irc); //renderer.toFile("e:\\mapinfo\\study\\png\\gradient.png") ; myimage = renderer.toImage(); mc = new mapCanvass(myimage); mc.repaint(); //essentially calls paint this.getContentPane().add(mc, BorderLayout.CENTER); this.setSize(800,600); this.setVisible(true); this.show(); renderer.dispose();}//end try catch (Exception e) { System.out.println("Error in execution"); e.printStackTrace(); } } //end Buffer Constructor public class mapCanvass extends JPanel { private Image m_img; mapCanvass (Image img)//constructor { m_img = img; }public void paint(Graphics g) { g.drawImage(m_img,0,0,this); }}//end class mapCanvass public static void main(String[] args) { StudyMapBuffer smb=new StudyMapBuffer(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -