?? addpointlabel.java~151~
字號:
package myfirstmap;
import com.mapinfo.mapxtreme.client.MapXtremeDataProviderRef;
import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
import java.awt.Color;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.unit.LinearUnit;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.mapinfo.graphics.Rendition;
import com.mapinfo.graphics.RenditionImpl;
import com.mapinfo.dp.Attribute;
import com.mapinfo.dp.PrimaryKey;
import com.mapinfo.mapj.FeatureFactory;
import com.mapinfo.dp.Feature;
import com.mapinfo.mapj.Layer;
import com.mapinfo.mapj.Layers;
import com.mapinfo.mapj.AbstractLayer;
import com.mapinfo.mapj.FeatureLayer;
import com.mapinfo.mapj.DataLayer;
import com.mapinfo.dp.TableInfo;
import com.mapinfo.dp.util.TableInfoImpl;
import com.mapinfo.coordsys.CoordSys;
import com.mapinfo.dp.annotation.AnnotationDataProviderHelper;
import com.mapinfo.dp.annotation.AnnotationTableDescHelper;
import com.mapinfo.mapj.BaseLabelProperties;
import com.mapinfo.dp.util.LocalDataProviderRef;
import java.util.Vector;
import com.mapinfo.dp.util.RewindableFeatureSet;
import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
import com.mapinfo.mapj.LabelProperties;
import com.mapinfo.unit.Distance;
public class AddPointLabel
{
private String mdfFilePath="e:\\MapInfo\\study\\mapmdf";
private String mdfFileName="states.mdf";
private String mapxtremeURL="http://901-32:8083/mapxtreme47/mapxtreme";
private int NUM_OF_COLORS = ImageRequestComposer.MAX_COLORS_TRUECOLOR;
private Color BACKGROUND_COLOR = Color.white;
private String layerName;
public AddPointLabel()
{
}
public void addLabelToPoint()
{
MapJ mapj=new MapJ();
Layers layers;
DataLayer dataLayer;
DoublePoint centerPoint=new DoublePoint(-108.8,42.5);
LinearUnit unit=LinearUnit.kilometer ;
mapj.setDistanceUnits(unit) ;
mapj.setDeviceBounds(new DoubleRect(0,0,800,600)) ;
Rendition rend = RenditionImpl.getDefaultRendition();
rend.setValue(Rendition.SYMBOL_MODE, Rendition.SymbolMode.FONT);
rend.setValue(Rendition.FONT_FAMILY, "MapInfo Cartographic");
rend.setValue(Rendition.FONT_SIZE, 6);
rend.setValue(Rendition.SYMBOL_STRING, String.valueOf((char)33));
// Rendition labelR.end = RenditionImpl.getDefaultRendition(); //required by feaFac constructor
Attribute fAtt[] = new Attribute[3]; //required by feaFac constructor
fAtt[0] = new Attribute("colvalue1");
fAtt[1]=new Attribute("colvalue2");
fAtt[2]=new Attribute("colvalue3");
//fAtt[1]=new Attribute("pkeyname");
PrimaryKey pkey = new PrimaryKey(fAtt); //required by feaFac constructor(below)
FeatureFactory feaFac = mapj.getFeatureFactory();
try
{ mapj.loadMapDefinition(mdfFilePath+"\\"+mdfFileName);
layers=mapj.getLayers() ;
dataLayer=(DataLayer)layers.get(0) ;
DoubleRect rect=dataLayer.getBounds() ;
double width=(rect.xmax -rect.xmin )/4;
double height=(rect.ymax -rect.ymin )/4;
mapj.setBounds(new DoubleRect(centerPoint,width,height)) ;//set the map to the size of 16*orginal size
/*創建一個點幾何體,它有三個屬性,到底有多少個屬性由自己設定但一定要和后面的表結構一致,一個幾何體在一個表中就相當
于一行;這兒用的pkey即主關鍵字,實際上它用到了三列,我們可以用一列,二列,三列都可以*/
Feature feature = feaFac.createPoint(centerPoint, null, null, fAtt,pkey);
Layer layer = null;
String[] ticolumns = new String[3];
ticolumns[0] = "colname1";
ticolumns[1]="colname2";
ticolumns[2] = "colname3";
//to be the primary key
int[] ctypes = new int[3];
ctypes[0] = TableInfo.COLUMN_TYPE_STRING; //specifies data-type in zeroth column
ctypes[1] = TableInfo.COLUMN_TYPE_STRING; //specifies data-type in first column
ctypes[2]=TableInfo.COLUMN_TYPE_STRING ;
int[] primarykey = new int[3];
primarykey[0]=0;
primarykey[1]=1;
primarykey[2]=2;
/*下面這條語句實際上就是定義一個表結構,從ticolumns可知它的三個列名,從ctypes可以知道每列的類型,
從3可以知道這個表有三列,記住ticolumns的大小必須和第六個參數一致,這兒為3,最難理解的是primarykey,經過我的
試驗我發現它是指用哪幾列作為主關鍵字列,這兒用了這三列*/
TableInfoImpl tii =
new TableInfoImpl("tablename", CoordSys.longLatNAD83, ticolumns,
ctypes, 3,primarykey, false);
AnnotationTableDescHelper annTDHelper = new AnnotationTableDescHelper("tablename");
AnnotationDataProviderHelper annDPHelper = new AnnotationDataProviderHelper(tii);
LocalDataProviderRef localDPRef = new LocalDataProviderRef(annDPHelper);
layer = mapj.getLayers().insert(localDPRef, annTDHelper, 0, "layernameforlabel");//add this
//annotation layer to mapj instance
//得到層名,沒有其它的意義,只是為了學習
// Layers layers=mapj.getLayers() ;
for(int layerindex=0;layerindex<layers.size() ;layerindex++)
{
AbstractLayer alayer=layers.get(layerindex) ;
FeatureLayer featureLayer=(FeatureLayer)layers.get(layerindex) ;
featureLayer.setAutoLabel(true) ;
layerName= alayer.getName() ;
// labelProperties.setPriority(10) ;
// int priority= labelProperties.getPriority() ;
}
//layer.setVisible(false);
layer.addFeature(feature);//把這點增加到該層中去
//create another feature again,if we use the previous fatt value,an error will occur,becuase these
//value will be used as the primary key at the same time
fAtt[0]=new Attribute("colvalue11");
fAtt[1]=new Attribute("colvalue12");
fAtt[2]=new Attribute("colvalue13");
centerPoint=new DoublePoint(-109.8,43.5);
//I don't know why it can be passed with the pkey unchanged
feature = feaFac.createPoint(centerPoint, null, null, fAtt,pkey);
PrimaryKey tmpPkey=feature.getPrimaryKey() ;
for(int i=0;i<feature.getAttributeCount() ;i++)
{
Attribute att=feature.getAttribute(i) ;
String str=att.getString() ;
str="wait";
}
layer.addFeature(feature) ;//add this feature to annotation layer
//set label style
LabelProperties labelProperties=layer.getLabelProperties() ;
Rendition labelRend=labelProperties.getRendition() ;
labelRend.setValue(Rendition.FONT_WEIGHT,2f);
labelRend.setValue(Rendition.SYMBOL_FOREGROUND ,Color.blue );
labelRend.setValue(Rendition.FILTER_EFFECTS ,Rendition.FilterEffects .HALO );
labelRend.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle .ITALIC );
labelProperties.setRendition(labelRend) ;
//create a scalable label
Distance distance=new Distance(28,LinearUnit.kilometer );
labelRend.setValue(labelRend.FONT_SIZE ,distance);
labelProperties.setMultiLineTextMode(LabelProperties.MULTILINE_TEXT_ON ) ;
labelProperties.setGeometryCalculationMode(LabelProperties.GEOMETRY_COMPUTED ) ;
// labelProperties.setZoomMax(10,LinearUnit.kilometer ) ;
//labelProperties.setZoomMin(5,LinearUnit.kilometer ) ;
//blp.setLabelExpression("\"colname:\""+ticolumns[0]);
//tell the lable to how to display information for us
System.out.println("labelProperties.DEFAULT_OFFSET .x ="+labelProperties.DEFAULT_OFFSET .x);
System.out.println("labelProperties.DEFAULT_OFFSET .y ="+labelProperties.DEFAULT_OFFSET .y);
BaseLabelProperties blp = new BaseLabelProperties(labelProperties);
// blp= layer.getLabelProperties();
blp.setLabelExpression("\"colname1:[\"+colname1+\"]\\ncolname2:[\"+colname2+\"]colname3:[\"+colname3+\"]\"");
layer.setLabelProperties(blp);
// layer.setLabelProperties(labelProperties) ;
mapj.setZoom(800000) ;
ImageRequestComposer imageRC =
ImageRequestComposer.create(mapj, 256, Color.white , "image/png");
MapXtremeImageRenderer render = new MapXtremeImageRenderer(mapxtremeURL);
render.render(imageRC);
render.toFile("E:\\MapInfo\\study\\png\\labelProperty3.png") ;
render.dispose() ;
}catch(Exception e)
{
System.out.println("get a "+e.getClass() +"\nwith message "+e.getMessage() ) ;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -